问题描述
我有点新AS3,但我有一个问题。
I'm a little new to AS3, but I have a question.
我有一个名为生命的变量。这是一个数字。我也有一个类。它被称为敌人。内的敌人级,有一个被称为collision_detection功能。我怎样才能改变从collision_detection,生命的价值?谢谢!
I have a variable called "lives". It is a Number. I also have a class. It is called "Enemy". Within the "Enemy" class, there is a function called "collision_detection". How can I change the value of "lives" from "collision_detection"? Thank you!
编辑:
我有一个敌人类。我需要在类的内部沟通,让主程序知道发生了碰撞。我怎样才能发送此消息主程序?
I have an Enemy class. I need to communicate from within the class to let the main program know that a collision occurred. How can I send this message to the main program?
编辑II:
下面是冲突功能:
public class Enemy extends MovieClip {
private var hoster : MovieClip;
private var life: Number;
public function temp_bad_thing(host : MovieClip , lives : Number) {
this.addEventListener(Event.ENTER_FRAME , check_collision);
hoster = host;
life = lives;
this.y = 0;
this.x = Math.floor(Math.random()*(551));
}
private function check_collision (evt:Event) : void{
if(this.hitTestObject(hoster) == true){
trace('COLLISION');
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME , check_collision);
}
}
}
现在我怎么能得到这个类来改变我的主要Flash文件变量的值?
Now how can I get this class to change the value of a variable in my main flash file?
推荐答案
如果变量是在同一个包中声明
,而不是在另一个类
,你应该能够分配给它没有做什么特别的事情。
If the variable is declared in the same package
and not in another class
you should be able to assign to it without doing anything special.
如果它在另一个类则声明生活
变量的静态公共变种生活:数
。这样,您就可以指定使用 otherClass.lives
的变量。 静态
变量只有一个拷贝会存在,无论你做多少个对象。
If it's in another class then declare the lives
variable as static public var lives: Number
. This way you can assign to the variable using otherClass.lives
. Only one copy of static
variable will exist regardless of how many objects you make.
这篇关于ActionScript类,多次沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!