for循环不在函数内部运行(For loop not running inside of a function)

编程入门 行业动态 更新时间:2024-10-25 22:25:10
for循环不在函数内部运行(For loop not running inside of a function)

我试图让一个角色(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 } }

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?

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 } }

最满意答案

你的问题是条件总是假(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 }

更多推荐

本文发布于:2023-07-17 18:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1148584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   loop   function   running

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!