Вопрос

I am wondering how to target specific objects/instances in flash as3. I have 2 objects on the stage, for now we'll call them obj1 and obj2 (with instance names). I am trying to have ob1's rotation speed based on obj2's y position. I want to place the code inside of obj1 so I figured if I wanted to target obj2 from inside obj1 I'd just use something like this.rotation = this.obj2.y / 10; but for some reason the thing just keeps it's rotation still. I used the "Target" button at the top of the native code editor but it still gave me the same this.obj2. Any ideas? Thanks in advance.

Это было полезно?

Решение

Assuming the two DisplayObjetcs are next to each other on stage, they have a common parent, so a way to "target" obj2 from obj1 would be:

this.rotation = this.parent.getChildByName("obj2").y / 10;

In other words, unless you set up your own references to other DisplayObjetcs like in Pan's answer, you can reference them through their position in the hierarchal display list.

Другие советы

If you want to update obj1's rotation based on obj2'y, you should call this.rotation = this.obj2.y / 10 in a ENTER_FRAME handler. Or when the obj2's y changes, call the function in obj1 to change rotation.

Assume A is obj1 class

Solution 1, use enter_frame event

class A {

    private var obj2:Object;

    public function class A($obj2:Object) {
        obj2 = $obj2;

        this.addEventListener(Event.ENTER_FRAME, changeRotation);
    }


    private function changeRotation(e:Event):void {
        this.rotation = this.obj2.y / 10;
    }
}

Solution 2, change rotation when obj2.y changed

class A {

    public function changeRotation(obj2:Object):void {
        this.rotation = this.obj2.y / 10;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top