我试图让一个角色(char)在游戏中分阶段跳跃,使用for循环跳转每次循环运行时的一部分。循环永远不会初始化。
跳转启动会追溯到输出控制台,但跳转号无法跟踪。
为什么会这样?
JumpHeight == 25
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void
{
if (jumping == false && char.hitTestObject(floor) == false)
{
char.y += gravity
}
}
function keyPressed(event:KeyboardEvent):void
{
if (event.keyCode == jumpKey)
{
jump()
}
}
function jump()
{
if (char.y >= groundY)
{
trace("Jump Starting")
jumping = true
for (jCycle = 0; jCycle == jumpHeight; jCycle++)
{
char.y -= gravity
trace("Jump No. " + jCycle)
}
jumping = false
}
}
答案 0 :(得分:0)
for循环条件出错:jCycle == jumpHeight
应为jCycle < jumpHeight
(或jCycle <= jumpHeight
)。
答案 1 :(得分:0)
你的问题是条件总是假(jCycle == 0!= jumpHeight)并且身体无法到达。 试试这个:
for (var jCycle:int = 0; jCycle <= jumpHeight; jCycle++)
{
//body
}