本文介绍了重力/速度&跳跃问题(AS3,平台游戏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有或多或少的工作重力&速度和命中测试 - 所以我不通过我的平台。



现在的问题是,只要我点击跳跃,命中测试停止工作,我穿过平台。如果我把我的角色设置到另一个更高的位置,我甚至不会掉下来。



任何人都可以帮我解决这个问题吗?

以下是我的代码:

  import flash.events.MouseEvent; 
import flash.events.Event;


var gravity:Number = 2;
var velocity:Number = 1.1;
var jumpPower:Number = 0;
var isJumping:Boolean = false;


stage.addEventListener(Event.ENTER_FRAME,touchPlatform);
player.addEventListener(Event.ENTER_FRAME,appeal);

函数touchPlatform(e:Event):void
{
if(kurz.hitTestObject(player))
{
hitPlatform = true;


else if(kurz2.hitTestObject(player))
{
hitPlatform = true;



$ b函数上诉(e:Event):void
{
gravity * = velocity;
player.y + =重力;

if(hitPlatform == true)
{
velocity = 0;
}

}


jump.addEventListener(MouseEvent.CLICK,doJump);
stage.addEventListener(Event.ENTER_FRAME,update);
函数doJump(e:MouseEvent):void
{
if(!isJumping)
{
jumpPower = 30;
isJumping = true;



函数更新(e:Event):void
{
if(isJumping)
{
player .y - = jumpPower;
jumpPower - = 2;

}

else
{
isJumping = false;


$ / code $


解决方案

问题是,一旦你开始跳跃,你永远不会停止!没有哪里(可以到达)你设置 isJumping 为false。此外,你的跳跃价值和你的引力现在一起运行,你只需要一个或另一个影响你的球员在任何给定的时间。



尝试像这样(见代码注释以供解释)

  import flash.events.MouseEvent; 
import flash.events.Event;


var gravity:Number = 2;
var velocity:Number = 1.1;
var jumpPower:Number = 0;
var isJumping:Boolean = false;

jump.addEventListener(MouseEvent.CLICK,doJump);
函数doJump(e:MouseEvent):void {
if(!isJumping){
jumpPower = 30;
isJumping = true;




//只需要一个ENTER_FRAME处理函数,
//然后你就可以更好地控制代码运行的顺序
//我已经把你的三合一称为gameLoop
stage.addEventListener(Event.ENTER_FRAME,gameLoop);

函数gameLoop(e:Event):void {
//这是一样的,我把你的2个if合并为一个。
if(kurz.hitTestObject(player)|| kurz2.hitTestObject(player)){
hitPlatform = true;


//你应该在赋值给玩家之前设置速度
if(hitPlatform == true){
velocity = 0;
gravity = 2; //你可能想把重力重置为默认值
isJumping = false; // SET现在已经跳到了错误的地步! < -------------
} else {
velocity = 1.1; //当你不在平台上时,你需要重置速度
}


//现在我们已经确定了速度,如果我们正在跳跃,让我们移动播放器如果(isJumping){
// //因为我们正在跳跃,使用jumpPower而不是重力
player.y - = jumpPower;
jumpPower - = 2;
} else {
if(!hitPlatform){
//因为我们不是在跳,而不是在平台上,所以使用重力。
gravity * = velocity;
player.y + =重力;
}
}
}


I'm currently trying to program a flash game for Android.

I have more or less working gravity & velocity and hit tests - so I don't fall through my platforms.

The problem now is, as soon as I hit "jump", the hit test stops working and i fall through the platforms. If I set my character to a different, higher position I don't even fall down.

Can anyone help me figure this out?

Here is my code:

import flash.events.MouseEvent;
import flash.events.Event;


var gravity:Number = 2;
var velocity:Number = 1.1;
var jumpPower:Number = 0;
var isJumping:Boolean = false;


stage.addEventListener(Event.ENTER_FRAME, touchPlatform);
player.addEventListener(Event.ENTER_FRAME, appeal);

function touchPlatform(e:Event):void
    {
        if(kurz.hitTestObject(player))
            {
                hitPlatform = true;
                }

        else if(kurz2.hitTestObject(player))
            {
                hitPlatform = true;
                }

        }

function appeal(e:Event):void
    {
        gravity *= velocity;
        player.y += gravity;

        if(hitPlatform == true)
        {
            velocity = 0;
            }

    }


jump.addEventListener(MouseEvent.CLICK, doJump);
stage.addEventListener(Event.ENTER_FRAME, update);
function doJump(e:MouseEvent):void
{
    if(!isJumping)
    {
        jumpPower = 30;
        isJumping = true;
        }
    }

function update(e:Event):void
    {
        if(isJumping)
        {
            player.y -= jumpPower;
            jumpPower -= 2;

            }

        else
        {
            isJumping = false;
            }
        }
解决方案

Your issue is that once you start jumping, you never stop! No where (that can be reached) do you set isJumping to false. Also, your jumping value and your gravity are currently running in tandem, you only want one OR the other affecting your player at any given time.

Try something like this (see code comments for explainations)

import flash.events.MouseEvent;
import flash.events.Event;


var gravity:Number = 2;
var velocity:Number = 1.1;
var jumpPower:Number = 0;
var isJumping:Boolean = false;

jump.addEventListener(MouseEvent.CLICK, doJump);
function doJump(e:MouseEvent):void {
    if(!isJumping){
        jumpPower = 30;
        isJumping = true;
    }
}


//Just one ENTER_FRAME handler is better,
//then you have more control over the order in which code gets run
//I've combined your three into one and called it gameLoop
stage.addEventListener(Event.ENTER_FRAME, gameLoop);

function gameLoop(e:Event):void {
    //this is the same, I just combined your 2 if's into one.
    if(kurz.hitTestObject(player) || kurz2.hitTestObject(player)){
        hitPlatform = true;
    }

    //you should set the velocity before assigning it to the player
    if(hitPlatform == true){
        velocity = 0;
        gravity = 2; //you probably want to reset gravity to default too
        isJumping = false; //SET isJumping to false now that you've hit a platform!  <-------------
    }else{
        velocity = 1.1; //you need to reset velocity when not on a platform
    }


    //Now that we've determined the velocity and if we're jumping, let's move the player the appropriate amount

    if(isJumping){
        //Since we're currently jumping, use the jumpPower instead of gravity
        player.y -= jumpPower;
        jumpPower -= 2;
    }else{
        if(!hitPlatform){
            //Since we're NOT JUMPING, and not on a platform, use gravity.
            gravity *= velocity;
            player.y += gravity;
        }
    }
}

这篇关于重力/速度&amp;跳跃问题(AS3,平台游戏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:22