ActionScript3, moving objects of type movieclip

你离开我真会死。 提交于 2020-01-06 06:53:19

问题


Here i am trying to create a new movieclip type object, which is moved when function mvBall is called. When i run the code i get this err: implicit coercion of a value with static type object to a possibly unrelated type flash.display:MovieClip. Later on i want to be able to make the ball bounce back when it colides with another object. I'm new to action script and don't really know how things work so any help would be appreciated. Here's the code:

private function frame(x:Event):void {
        var ball:MovieClip = new MovieClip();
        ball.addEventListener(Event.ENTER_FRAME, animate);
        ball.graphics.beginFill(0xff0000); 
        ball.graphics.drawCircle(100, 100, 15); 
        ball.graphics.endFill(); 
        stage.addChild(ball); 
    }

    private function animate(ev:Event):void {
        mvBall(ev.target);
    }

    private function mvBall(mc:MovieClip) {
        mc.x += 10;
    }

回答1:


You need to cast the target to MovieClip

private function animate(ev:Event):void {
    mvBall(ev.target as MovieClip);
}

With that said it is better to just have one ENTER_FRAME handler and animate your objects in there.

stage.addEventListener(Event.ENTER_FRAME, animate);

private function animate(ev:Event):void
{
    mvBall(myBall);
    //other object animations
}



回答2:


You are getting this error because the target property of the Event class is of type object.

In order to not throw the error, you need to cast it as a MovieClip:

mvBall(ev.target as MovieClip);

or

myBall(MovieClip(ev.target));

Something else to consider, is the difference between an Events target and currentTarget properties. If you ball had multiple layers/object inside it (sprites or other movieClips), the target would be whichever one of those sub-elements had the mouse over it during the click. currentTarget refers to the object that you've attached the listener to. In your case they may be the same (if your ball doesn't have any movie clips inside it), but your code could have unexpected results if you have sub-movieClips inside your ball.



来源:https://stackoverflow.com/questions/13260618/actionscript3-moving-objects-of-type-movieclip

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