AS3:如何制作类似邮票的效果[特别是我正在使用的编码]?

时间:2014-05-07 03:55:04

标签: actionscript-3 flash actionscript flashdevelop

我对Flash和Actionscript非常陌生。我抓住一些元素的时候非常糟糕。

我目前正在制作一个基于我在网上找到的初学者教程的程序,并且我已将其编辑到目前为止我想要的除了制作类似邮票的效果之外。

到目前为止发生的事情很简单:它生成一个笑脸矢量图像,如果用户点击它,它会跟随光标,直到用户再次点击它将停留的位置,不再跟随光标。 / p>

我要做的是修改click事件,以某种方式不仅放下向量,而且基本上创建一个新的跟随光标并允许另一个成为位置,依此类推等等。

我到目前为止的代码如下:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.ui.Keyboard;

    /**
     * ...
     * @author Lyon's Den Gaming
     */
    public class Main extends Sprite 
    {
        public var happyface:Sprite;
        public var stampcount:Number = 0;
        public var happyfacecursor:Boolean;

        public var mouseListener:Object = new Object();

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            happyface = new Sprite();
            addChild(happyface);

            //This will draw the main circle O
            happyface.graphics.beginFill(0xf2f75b);
            happyface.graphics.lineStyle(6, 0x000000);
            happyface.graphics.drawCircle(800 / 2, 600 / 2, 200);
            happyface.graphics.endFill();

            //This will draw the eyes ^^
            happyface.graphics.lineStyle(8);
            happyface.graphics.moveTo(325, 170);
            happyface.graphics.lineTo(325, 245);

            happyface.graphics.lineStyle(8);
            happyface.graphics.moveTo(466, 170);
            happyface.graphics.lineTo(466, 245);

            //This will draw the mouth :D
            happyface.graphics.lineStyle(8);
            happyface.graphics.moveTo(275, 340);
            happyface.graphics.lineTo(525, 340);

            happyface.graphics.lineStyle(8);
            happyface.graphics.beginFill(0xdb3d3d);
            happyface.graphics.moveTo(275, 340);
            happyface.graphics.cubicCurveTo(280, 500, 520, 500, 525, 340);
            happyface.graphics.endFill();

            //this scales it down and places it where I want

            happyface.scaleX = .15;
            happyface.scaleY = .15;
            happyface.x = 335;
            happyface.y = 265;


            //This will add mouse input!
            stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseMoveHappyFace);
            happyface.addEventListener(MouseEvent.CLICK, SelectHappyFace);




        }   
        public function MouseMoveHappyFace(e:MouseEvent):void
        {
        if (happyfacecursor)
        {

            happyface.x = mouseX - happyface.width;
            happyface.y = mouseY - happyface.height / (1.4);
        } else {
            happyfacecursor = false;
            addChild(happyface);
        }

        }

        public function SelectHappyFace(e:MouseEvent):void
        {

            if (happyfacecursor)
            happyfacecursor = false;
            else
            happyfacecursor = true;

        }






    }

}

1 个答案:

答案 0 :(得分:0)

为什么不使用拖放方法?单击happyface结束或开始拖动时:

编辑:您不会因此而需要MouseMoveHappyFace功能:

编辑2:在这里,您有一种新方法,可以一次创建一个快乐的脸。我没有尝试过,但它应该有用。

   public var happyface:Sprite;
   public var stampcount:int = 0;
   public var happyfacecursor:Boolean;

   public function Main():void {
      if (stage) init();
      else addEventListener(Event.ADDED_TO_STAGE, init);
   }

   private function init(e:Event = null):void {
      removeEventListener(Event.ADDED_TO_STAGE, init);
      // entry point

      // Create the first stamp
      addNewHappyFace();
   }

   public function addNewHappyFace():void {

      // This method creates a new happyFace, adds it and asigns it to 
      // the global happyface variable

      var aux = new Sprite();
      this.addChild(aux);

      stampcount++;

      //This will draw the main circle O
      aux.graphics.beginFill(0xf2f75b);
      aux.graphics.lineStyle(6, 0x000000);
      aux.graphics.drawCircle(800 / 2, 600 / 2, 200);
      aux.graphics.endFill();

      //This will draw the eyes ^^
      aux.graphics.lineStyle(8);
      aux.graphics.moveTo(325, 170);
      aux.graphics.lineTo(325, 245);
      aux.graphics.lineStyle(8);
      aux.graphics.moveTo(466, 170);
      aux.graphics.lineTo(466, 245);

      //This will draw the mouth :D
      aux.graphics.lineStyle(8);
      aux.graphics.moveTo(275, 340);
      aux.graphics.lineTo(525, 340);
      aux.graphics.lineStyle(8);
      aux.graphics.beginFill(0xdb3d3d);
      aux.graphics.moveTo(275, 340);
      aux.graphics.cubicCurveTo(280, 500, 520, 500, 525, 340);
      aux.graphics.endFill();

      //this scales it down and places it where I want
      aux.scaleX = .15;
      aux.scaleY = .15;
      aux.x = 335;
      aux.y = 265;

      //This will add mouse input!
      aux.addEventListener(MouseEvent.CLICK, SelectHappyFace);

      happyface = aux;
   }

   public function SelectHappyFace(e:MouseEvent):void{
      if (happyfacecursor) }
         happyface.startDrag(); // To make the object follow the mouse
         happyfacecursor = false;
      } else {
         happyface.stopDrag(); // To drop the object where you want

         // This will prevent that when clicking on an already placed stam i would
         // make the last one created to move
         happyface.removeEventListener(MouseEvent.CLICK, SelectHappyFace);         

         happyfacecursor = true;

         // Now create and add a new HappyFace clip here
         addNewHappyFace()
      }
   }