2012-07-09 52 views
0

我不知道這個代碼的想法,請幫助我如何讓圖像在Flex上隨機移動?

+4

我不知道這個問題的想法,請幫助我們回答你的問題。 – GokcenG 2012-07-09 21:34:03

+4

@GokcenG的意思是說:提出一個真正的問題,即描述您的問題,向我們展示您嘗試過的一些代碼,任何事情 - 也許我們可以提供幫助。並請閱讀常見問題解答:http://stackoverflow.com/faq – weltraumpirat 2012-07-09 22:00:14

+0

其中一個邏輯概念,你可以在下面找到: - http://stackoverflow.com/questions/11010974/dropdown-following-moving-calloutbutton/11099885# 11099885 – 2012-07-10 11:58:00

回答

0

一些簡單的例子:

<?xml version="1.0" encoding="utf-8"?> 
    <s:Application 
     xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" 
     creationComplete="creationCompleteHandler()"> 
     <s:Image 
      id="image" 
      width="100" 
      height="100" 
      x="50" 
      y="50" 
      backgroundColor="red"/> 

     <fx:Script> 
      <![CDATA[ 
       import mx.events.EffectEvent; 

       import spark.effects.Move; 

       private function creationCompleteHandler():void 
       { 
        _moveEffect = new Move(); 
        _moveEffect.duration = 450; 
        _moveEffect.target = image; 

        var timer:Timer = new Timer(1000); 
        timer.addEventListener(TimerEvent.TIMER, timerHandler); 
        timer.start(); 
       } 

       private function timerHandler(event:TimerEvent):void 
       { 
        var xDest:int = image.x + getRandom(-100, 100); 
        var yDest:int = image.y + getRandom(-100, 100); 

        if(xDest < 0) xDest = 0; 
        else if(xDest > width) xDest = parent.width; 

        if(yDest < 0) yDest = 0; 
        else if(yDest > height) yDest = parent.height; 

        _moveEffect.xFrom = image.x; 
        _moveEffect.xTo = xDest; 
        _moveEffect.yFrom = image.y; 
        _moveEffect.yTo = yDest; 

        _moveEffect.play(); 
       } 

       private function getRandom(lowLimit:Number, highLimit:Number):int 
       { 
        return Math.floor(Math.random() * (1 + highLimit - lowLimit)) + lowLimit; 
       } 

       private var _moveEffect:Move; 
      ]]> 
     </fx:Script> 
    </s:Application>