ActionScript Class Number Communication

假如想象 提交于 2019-12-12 03:15:55

问题


I'm a little new to AS3, but I have a question.

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!

EDIT:

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?

EDIT II:

Here is the collision function :

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); 

        }

    }

}

Now how can I get this class to change the value of a variable in my main flash file?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/9728927/actionscript-class-number-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!