¿Cómo mover una imagen aleatoriamente en la pantalla de una vista en mobile flex?

I have to create an mobile flex application with tabbed navigator view. One of the view must satisfy this condition: when the view is selected, an image will appear for one second and then disappear for half a second, then reappear at a random position on the screen of the view. This will repeat until another view is selected.

I'm new to Mobile Flex and I need your help.

Gracias de antemano.

Best regards, HBLE

preguntado el 28 de agosto de 12 a las 11:08

1 Respuestas

  1. Use enterFrame event or a Timer to hide/show the image.
  2. Set the image x and y properties to show the image at a specific position
  3. Use Math.random() to generate a random number in the interval [0,1]

Important: When tab is active call init(); When changing to other tab do not forget to stop the timer and remove event listeners. (for performance reasons and to avoid memory leaks)

Código de muestra:

var isVisible:Boolean = false;

function init():void
{
   // we show / hide with a delay of 1 second
   var t:timer = new Timer(1000);
   t.addEventListener(TimerEvent.Timer, onTimer);
   t.start();
}

function onTimer(event:TimerEvent):void
{
   if(isVisible)
   {
       hideImage();
   }
   else
   {
       showAndMoveImage();
   }

   isVisible = !isVisible;
}

function hideImage():void
{
    myImage.visible = false;
}


function showAndMoveImage():void
{
    // we reposition image in screen, assume image size is smaller then screen
    myImage.x  = Math.random() * (stage.width - myImage.width);
    myImage.y  = Math.random() * (stage.height - myImage.height);

    myImage.visible = true;
}

Respondido 28 ago 12, 12:08

I don't think this code wil work in Flex, which doesn't have a Timeline. You need a proper Class. - Amy Blankenship

I try your code @Adrian Pirvulescu, it works in my flex mobile application. However, it must be modified to be adapted to my codes. In addition, your code have a problem in displaying the image. sometimes, it seems to me that the image goes out of the screen frame (it disappears for 3-4 seconds from the view). - Hai-Binh LE

@AmyBlankenship put this code in your class and it will work. Also if you have a MXML file, you can add this between your <script>... tags - Adrián Pirvulescu

@Hai-BinhLE can you please trace(stage.witdth) and trace(myImage.width) and also for the heigth to see if there are the correct values ? Also for sure you have to adapt it to your app. This is just a rough sample of how can you do some things. Also you did not posted any code so... I have no idea how can I adapt this to your code. - Adrián Pirvulescu

@AdrianPirvulescu: in fact, in the script portion of myView.mxml, instead of using stage.with, I use this.width/height, everything seems to be ok to me, the image doesn't go out of the screen. - Hai-Binh LE

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.