开发者

Change properties of an object in real-time

开发者 https://www.devze.com 2023-03-31 04:16 出处:网络
It seems a naive question but I\'ve realized I don\'t how to achieve this. I have a Sprite object that I called three methods,

It seems a naive question but I've realized I don't how to achieve this.

I have a Sprite object that I called three methods,

myObject.graphics.drawRect(params, params params, params); 
myObject.graphics.beginFill(params);

The problem is that I also called

addEventListeneter(Event.ENTER_FRAME, myfunc);

in

myfunc(e:Event):void
{
myObject.x += 5;
}

I update the x coordinate of myObject.

Suppose I have a button or I dispatch a new event after some seconds later, it doesn't matter how, what matters is how I can change myObject color while is running the screen. I thought something like removing myObject from screen and adding another object, almost similar, but now a red rectangle.

However, I would have to know exactly how much time passed since the file started until the button is pressed in order to do some maths with the speed of myObject to really know where it has to be added(at the same place where myObject was removed when I pressed a certain button).

myNewObje开发者_如何学Cct.graphics.drawRect(the x coordinate where myObject was removed when I pressed the button so that I remove the old black rectangle and add a generic red rectangle, params params, params);

myNewObject.graphics.beginFill(new param);

But how come ? Is there an easier way to accomplish this ?

Thanks.


You can clear the graphics from the old Sprite and draw a new rect with new color:

  // After the event is dipatched:
  function eventHandler(event:Event):void
  {
      myObject.graphics.clear();
      myObject.graphics.beginFill(white);
      myObject.graphics.drawRect(params, params, params, params); 
      myObject.graphics.endFill();
  }

The sprite will continue the animation with the new color.

0

精彩评论

暂无评论...
验证码 换一张
取 消