开发者

Actionscript eventdispatcher in Paint Program

I'm writing a MS Paint like simple program in Flash with pure ActionScript 3.0

  • A toolbar that allows you to select what shape (star, heart…) you want to draw
  • Another toolbar for color options, where each button is a color (red, green). there are 10 colors total.
  • Then there's the canvas, its where the shapes will be drawn when 开发者_运维技巧clicking.

I have the following classes

class Main extends sprite
class ColorButton extends sprite
class ShapeButton extends sprite

class Star() extends sprite // star object to be added to Main (canvas)
class Heart() extends sprite // heart object to be added to Main (canvas)

Since I'm new to AS Im not sure how to / where to save the state (user selection). How can clicking a button change what a click on the canvas does? I think I need to use EventDispatcher? Could you guys point me to the right direction?


you have two choices, either you can make a static class that can hold all of the information, or it can be held in your canvas class, it depends a lot on other functionalities but for simplicities sake i would use the canvas.

to use the EventDispatcher you need to create and listen for Events, for example you can listen to the mouse up event by using:

ShapeButton.addEventListener(MouseEvent.MOUSE_UP, changeShape);

which will then call the changeShape function, which in this case would probably set a variable of the shape of the "brush". i would personally use the variable to hold a reference to the sprite that would be painting the shape, eg:

private var paintShape:Class;

public function Main(){
    paintShape = Star; // Make a star brush
    addEventListener(MouseEvent.MOUSE_UP, paintObject);
    //...
}

private function paintObject(ev:MouseEvent){
   var newShape = new paintShape() as Sprite;
   newShape.x = mouseX;
   newShape.y = mouseY;
   addChild(newShape);
}

see livedocs for an overview of Events

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜