在重复我的大学项目时,如何使 AS3 Var Loop 在 Y 轴上移动速度
How Can I Make AS3 Var Loop move speed on the Y Axis on repeat my university project
我正在尝试让汽车图像在 y 轴上向下移动屏幕并使其重复并与另一个对象碰撞
//creates the new Car
for (var c:int=0; c<8; c++){
var newcar = new car();
newcar.x = 55*c;
newcar.y = 100;
EntityArray.push(newcar);
stage.addChild(newcar);
trace("Car Created"+c)
}
如何让它与以下内容发生冲突并将其从屏幕上移除
//creates the new Frog
for (var f:int=0; f<1; f++){
var newfrog = new frog();
newfrog.x = 210;
newfrog.y = 498;
EntityArray.push(newfrog);
stage.addChild(newfrog);
trace("Frog Created"+f)
}
[图片][1][1]: https://i.stack.imgur.com/Ihsfx.png
虽然我很高兴听到今天他们仍然在大学里告诉你 ActionScript,但这有点难
在这里给你建议,因为我还不知道他们涵盖了什么。
一般来说,您可以通过一个简单的游戏循环来实现这一点,该循环会定期运行并且最简单
形式:
- 检查用户输入(在您的情况下很可能是按 left/right 来移动青蛙)
- 更新游戏状态(移动汽车和青蛙;检查碰撞)
- 将所有内容绘制到屏幕上
为了创建周期循环,Flash/ActionScript 提供了一个名为 ENTER_FRAME 的强大事件。一旦开始,它
将以电影的帧速率触发。所以如果你将你的电影设置为 60fps,它会执行它的回调函数
大约每 17 毫秒。
我假设您的 Frog 和 Car 实例扩展了 Flash 的 Sprite 或 MovieClip class - 所以碰撞检测也很不错
很容易,因为您可以使用继承的 hitTestObject() 方法。
为了让事情变得更简单,我建议您不要将对青蛙实例的引用放在 EntityArray 中。
最好使用全局参考。 (此外,您不需要 for 循环,因为只有一只青蛙)
作为另一个旁注,class名称以大写字母开头是很常见的。
private var newfrog:frog; // defines a class variable we can access anywhere inside our class
//Later on instantiate new cars and the frog:
for (var c:int=0; c<8; c++){
var newcar = new car();
newcar.x = 55*c;
newcar.y = 100;
EntityArray.push(newcar);
stage.addChild(newcar);
}
newfrog = new frog();
newfrog.x = 210;
newfrog.y = 498;
stage.addChild(newfrog);
addEventListener(Event.ENTER_FRAME, loop); // register an ENTER_FRAME listener for the main game loop
private function loop(e:Event):void
{
var tempCar:car;
for(var a:int=0;a<EntityArray.length;a++)
{
tempCar=EntityArray[a]; // get a car from the EntityArray
tempCar.y++; // move it down on screen
if(tempCar.y>600) // if it's vertical position is greater than 600...
{
tempCar.y=0; // ...move it back to the top
}
if(newfrog.hitTestObject(tempCar)) // evaluates to true, if a car and the frog's bounding boxes overlap
{
trace("there's a collision!"); // do something
}
}
}
我正在尝试让汽车图像在 y 轴上向下移动屏幕并使其重复并与另一个对象碰撞
//creates the new Car
for (var c:int=0; c<8; c++){
var newcar = new car();
newcar.x = 55*c;
newcar.y = 100;
EntityArray.push(newcar);
stage.addChild(newcar);
trace("Car Created"+c)
}
如何让它与以下内容发生冲突并将其从屏幕上移除
//creates the new Frog
for (var f:int=0; f<1; f++){
var newfrog = new frog();
newfrog.x = 210;
newfrog.y = 498;
EntityArray.push(newfrog);
stage.addChild(newfrog);
trace("Frog Created"+f)
}
[图片][1][1]: https://i.stack.imgur.com/Ihsfx.png
虽然我很高兴听到今天他们仍然在大学里告诉你 ActionScript,但这有点难 在这里给你建议,因为我还不知道他们涵盖了什么。 一般来说,您可以通过一个简单的游戏循环来实现这一点,该循环会定期运行并且最简单 形式:
- 检查用户输入(在您的情况下很可能是按 left/right 来移动青蛙)
- 更新游戏状态(移动汽车和青蛙;检查碰撞)
- 将所有内容绘制到屏幕上
为了创建周期循环,Flash/ActionScript 提供了一个名为 ENTER_FRAME 的强大事件。一旦开始,它 将以电影的帧速率触发。所以如果你将你的电影设置为 60fps,它会执行它的回调函数 大约每 17 毫秒。 我假设您的 Frog 和 Car 实例扩展了 Flash 的 Sprite 或 MovieClip class - 所以碰撞检测也很不错 很容易,因为您可以使用继承的 hitTestObject() 方法。 为了让事情变得更简单,我建议您不要将对青蛙实例的引用放在 EntityArray 中。 最好使用全局参考。 (此外,您不需要 for 循环,因为只有一只青蛙)
作为另一个旁注,class名称以大写字母开头是很常见的。
private var newfrog:frog; // defines a class variable we can access anywhere inside our class
//Later on instantiate new cars and the frog:
for (var c:int=0; c<8; c++){
var newcar = new car();
newcar.x = 55*c;
newcar.y = 100;
EntityArray.push(newcar);
stage.addChild(newcar);
}
newfrog = new frog();
newfrog.x = 210;
newfrog.y = 498;
stage.addChild(newfrog);
addEventListener(Event.ENTER_FRAME, loop); // register an ENTER_FRAME listener for the main game loop
private function loop(e:Event):void
{
var tempCar:car;
for(var a:int=0;a<EntityArray.length;a++)
{
tempCar=EntityArray[a]; // get a car from the EntityArray
tempCar.y++; // move it down on screen
if(tempCar.y>600) // if it's vertical position is greater than 600...
{
tempCar.y=0; // ...move it back to the top
}
if(newfrog.hitTestObject(tempCar)) // evaluates to true, if a car and the frog's bounding boxes overlap
{
trace("there's a collision!"); // do something
}
}
}