1
package com.basics.stick
{

import flash.display.MovieClip;
import flash.display.Stage;
import com.basics.senocular.utils.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;



public class StickMan extends MovieClip
{
    private var stageRef:Stage;
    private var key:KeyObject;
    private var speed:Number = 2;
    private var vx:Number = 0;
    private var vy:Number = 0;
    private var friction:Number = 0.93;
    private var maxspeed:Number = 8;
    private var fireTimer:Timer;
    private var canFire:Boolean = true;


    public function StickMan(stageRef:Stage)
    {
        this.stageRef = stageRef;
        key = new KeyObject(stageRef);

        fireTimer = new Timer(300,1);
        fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);

        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
    }



    private function fireTimerHandler(e:TimerEvent):void
    {
        canFire = true;
    }

    private function fireBullet():void
    {
        if (canFire)
        {
            stageRef.addChild(new Bullet(stageRef,x+vx+35, y+10));
            canFire = false;
            fireTimer.start();
        }
    }

    public function loop(e:Event):void
    {

        if (key.isDown(Keyboard.LEFT))
        {
            vx -=  speed;
        }
        else if (key.isDown(Keyboard.RIGHT))
        {
            vx +=  speed;
        }
        else
        {
            vx *=  friction;

        }
        if (key.isDown(Keyboard.UP))
        {
            vy -=  speed;
        }
        else if (key.isDown(Keyboard.DOWN))
        {
            vy +=  speed;
        }
        else
        {
            vy *=  friction;
        }

        if (key.isDown(Keyboard.SPACE))
        {
            fireBullet();
        }
        x +=  vx;
        y +=  vy;


        if (vx > maxspeed)
        {
            vx = maxspeed;
        }
        else if (vx<-maxspeed)
        {
            vx =  -  maxspeed;
        }
        if (vy > maxspeed)
        {
            vy = maxspeed;
        }
        else if (vy<-maxspeed)
        {
            vy =  -  maxspeed;

        }


        if (x > stageRef.stageWidth)
        {
            x = stageRef.stageWidth;
            vx =  -  vx;
        }
        else if (x<0)
        {
            x = 0;
            vx =  -  vx;

        }
        if (y > stageRef.stageHeight)
        {
            y = stageRef.stageHeight;
            vy =  -  vy;
        }
        else if (y<0)
        {
            y = 0;
            vy =  -  vy;

        }
    }
}
}

我想要的是当我按下向上按钮时,不仅仅是向上,而是跳跃,然后下降到特定的 y。我在网上搜索,但是大部分教程或其他用户都将代码放在字符中。但是我想要的是修改上面的代码,并使字符跳转。任何指导或任何东西都非常受欢迎。

提前感谢您的时间!:)

4

1 回答 1

0

您对 vy 的逻辑应该与 vx 不同。你需要为 vy 做的就是突然将它设置为一个相当大的负值,然后不断地添加一个小的值来模拟重力。这就是重力:垂直速度的变化率(加速度)。

您已经知道如何使用标志,因此您可以制作一个用于跳跃的标志,就像您为 canFire 所做的那样。

if (canJump)
{
    if(key.isDown(Keyboard.UP))
    {
        vy = -8; // for example this value, notice that it is assignment and not "-="
        groundy = y; // this a class private var, to remember the ground level
        canJump = false;
    }
    else if (key.isDown(Keyboard.DOWN))
    {
        // Forget about this, this can be erased
        // vy +=  speed;
    }
    else
    {
        // Also forget about this, it can be erased
        //vy *=  friction;
    }
}
else
{
    vy += 0.2; // an example value that you can tweak. this simulates gravity

    // The character is going down and now hits the ground
    if(y > groundy) {
        vy = 0;
        y = groundy;
        canJump = true;
    }
}

// ...

x +=  vx;
y +=  vy;

// ...

上面的代码没有经过真正的测试,但它应该可以工作,这是基本的游戏编程,实际上只是基本的计算物理,比如速度和加速度。

我想这是vx您想要在游戏中获得的马里奥平台游戏效果(在冰上,因为您的处理方式)。当然,如果你想要像 Final Fight(垂直水平行走以及跳跃)这样的游戏玩法,逻辑会非常不同。

于 2012-04-19T08:21:40.007 回答