for循环不在函数内部运行(For loop not running inside of a function)
我试图让一个角色(char)在游戏中分阶段跳跃,使用for循环跳转每次循环运行时的一部分。 循环永远不会初始化。
跳转开始跟踪到输出控制台,但跳转编号无法跟踪。
为什么是这样?
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 } }JumpHeight == 25
I am trying to make a character (char) jump in stages in a game using a for loop to jump a part of the way each time the loop run. The loop never initializes.
Jump starting is traced to the output console but the jump No. does not get traced.
Why is this?
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 } }JumpHeight == 25
最满意答案
你的问题是条件总是假(jCycle == 0!= jumpHeight)并且身体无法访问。 尝试这个:
for (var jCycle:int = 0; jCycle <= jumpHeight; jCycle++) { //body }Your problem is that for condition is always false (jCycle == 0 !=jumpHeight) and body is unreachable. Try this:
for (var jCycle:int = 0; jCycle <= jumpHeight; jCycle++) { //body }更多推荐
发布评论