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。我在线搜索,但大多数教程或其他用户将代码放在角色中。但是我想要的是修改上面的代码,并使字符跳转。任何指导或任何指导都非常受欢迎。
thanx提前你的时间! :)
答案 0 :(得分: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;
// ...
上面的代码并没有真正经过测试,但它应该可行,这是基本的游戏性编程,而且实际上只是关于基本的计算物理,如速度和加速度。
我认为这是你想要进入游戏的Mario平台游戏效果(在冰上,因为你处理vx
的方式)。当然,如果您想要像Final Fight这样的游戏玩法(垂直行走和水平并且也可以跳跃),逻辑将会非常不同。