【问题标题】:Gravity / velocity & jump issue (AS3, platformer)重力/速度和跳跃问题(AS3,平台游戏)
【发布时间】:2015-06-28 20:59:50
【问题描述】:

我目前正在尝试为 Android 编写一个 Flash 游戏。

我或多或少地进行了重力和速度以及撞击测试 - 所以我不会跌倒在我的平台上。

现在的问题是,只要我点击“跳跃”,命中测试就会停止工作,我会从平台上掉下来。如果我将我的角色设置为不同的更高位置,我什至不会跌倒。

谁能帮我解决这个问题?

这是我的代码:

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;
            }
        }

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    你的问题是,一旦你开始跳跃,你就永远不会停止!你在哪里(可以到达)将isJumping 设置为false。此外,您的跳跃值和您的重力目前正在同步运行,您只希望在任何给定时间影响您的玩家。

    尝试这样的事情(请参阅代码 cmets 以获得解释)

    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;  
            }
        }
    }
    

    【讨论】:

    • 哦..是的,这是有道理的!谢谢,我能够解决它! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多