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これは、あなたがゲームで手に入れたいマリオのプラットフォーマー効果 (氷上で、あなたの扱い方から) だと思います。もちろん、ファイナル ファイトのようなゲームプレイ (垂直方向水平方向の歩行とジャンプ) が必要な場合、ロジックはまったく異なります。

于 2012-04-19T08:21:40.007 に答える