AS3 让敌人向鼠标移动
package {
import enemies.Enemy;
import flash.display.Sprite;
import flash.events.*;
public class Main extends Sprite {
// a place to store the enemy
public var enemy:Enemy;
private function handleEnterFrame(e:Event):void {
tweenIt(enemy.x, mouseX, 2);
}
private function tweenIt(variable:Number, target:Number, speed:Number):void{
if (variable < target) {
variable += speed;
}
if (variable > target) {
variable -= speed;
}
}
// this is the first code that is run in our application
public function Main():void {
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
// we create the enemy and store him in our variable
enemy = new Enemy();
// we add the enemy to the stage
addChild(enemy)
enemy.x = Math.random() * stage.stageWidth;
enemy.y = Math.random() * stage.stageHeight;
}
}
}
敌人类中嵌入了一个位图。我正在使用FlashDevelop来编程。当我做一些像敌人.x+=1这样的事情时,它可以工作,但是当我尝试使用我的补间脚本时,无论鼠标的位置如何,敌人都会保持静止。 谢谢你, 布洛布斯塔
package {
import enemies.Enemy;
import flash.display.Sprite;
import flash.events.*;
public class Main extends Sprite {
// a place to store the enemy
public var enemy:Enemy;
private function handleEnterFrame(e:Event):void {
tweenIt(enemy.x, mouseX, 2);
}
private function tweenIt(variable:Number, target:Number, speed:Number):void{
if (variable < target) {
variable += speed;
}
if (variable > target) {
variable -= speed;
}
}
// this is the first code that is run in our application
public function Main():void {
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
// we create the enemy and store him in our variable
enemy = new Enemy();
// we add the enemy to the stage
addChild(enemy)
enemy.x = Math.random() * stage.stageWidth;
enemy.y = Math.random() * stage.stageHeight;
}
}
}
The enemy class has a bitmapped embeded into it. I am using FlashDevelop to program. When I do something like enemy.x+=1 it works, but when I try using my tween it script the enemy stands still no matter what the position of the mouse.
Thank you,
Blobstah
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不是 AS3 开发人员,因此如果您的代码有任何问题,我无法帮助您,但如果您不确定如何以数学方式将敌人移向鼠标,请按以下方法操作。 (这不是代码,只是您想要计算的一般要点。我相信您可以将其转换为 AS3。)
首先,找到敌人和您的鼠标之间的距离。
然后,找到将敌人指向鼠标所需的旋转。
最后,这是您想要放入 tweenIt 函数中的内容,以将敌人移向鼠标(每个函数调用 3 个像素)。
应该就是这样!我认为递归 因为这是我学会如何做到这一点的地方。
I'm not an AS3 developer so I can't help you if anything's wrong with your code, but if you're unsure of how to mathematically move an enemy towards the mouse, here's how. (This isn't the code, just the general jist of what you want to calculate. I'm sure you can convert it to AS3.)
First, find the distance between the enemy and your mouse.
Then, find the rotation needed to point the enemy towards the mouse.
And lastly, here is what you want to put inside your tweenIt function to move the enemy towards the mouse (at 3 pixels per function call).
And that should be it! I give credit to Be Recursive because it's where I learned how to do this.
您将敌人的
x
位置的值传递给您的tweenIt
函数,更改该值,然后丢弃结果。换句话说,
variable
是一个与enemy.x
不同的变量,尽管它的起始值来自enemy.x< /代码>。
解决此问题的一种方法是将参数更改为实际敌人的引用:
You're passing in the value of the enemy's
x
position to yourtweenIt
function, changing that value, then throwing the result away.In other words,
variable
is a different variable thanenemy.x
, even though it got its starting value fromenemy.x
.One way to fix this is by changing the parameter to be a reference the the actual enemy:
因此,为了添加卡梅伦的答案,
您可以创建一个更通用的函数来更改变量。我将在下面演示一个小示例
上面的函数将更新您想要的变量的当前值,因此如果您输入以下内容:
这会将敌人对象的宽度更新为 200 :) 这应该可以解决问题: )
So, To add to the answer of Cameron
you can make a more general function to change variables. I will demonstrate a small example below
The above function will update the current value of the variable you want, so if you would type the following:
this would update the width of your enemy-object to 200 :) And this should do the trick :)