Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
gyubok
Posts: 517
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Moving object

Hi all, I'm trying to move an Loader object across the screen and having problem. It is probably very noobish to ask for this type of question but I'm very new to ActionScript and the internet does not offer clear answer.

 

I've found many people using the "movie clip" to simulate a movement but I don't know how to use the function (or even find it).

 

The code I have right now to load the image onto the stage is:

 

 

var obj: Loader = new Loader();
obj.load(new URLRequest("obj.png"));
obj.x = 354;
obj.y = 142;
addChild(obj);

 

 

Please give me some kind of documentation or sample code! Thanks!!

Please use plain text.
Developer
UberschallSamsara
Posts: 920
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Moving object

You can use e.g. the Tweener class to do animations, optionally using non linear easing curves.

Please use plain text.
Developer
tags07
Posts: 386
Registered: ‎12-12-2010
My Device: BB Style
My Carrier: Sprint

Re: Moving object

http://www.streamhead.com/how-to-use-images-in-actionscript-3-with-flashdevelop-and-some-other-as3-t...

 

 

package  {
 
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.ui.Mouse;
 
	public class Test extends Sprite {
 
		[Embed(source='library/crosshair.png')]
		private var crosshairClass:Class;
		private var crosshair:Bitmap = new crosshairClass ();
 
		public function Test() {
			Mouse.hide();
 
			crosshair.height = 30;
			crosshair.width = 30;
			addChild(crosshair);
 
			stage.addEventListener(Event.ENTER_FRAME, mouseMove);
		}
 
		private function mouseMove(evt:Event):void {
			crosshair.x = mouseX - 15;
			crosshair.y = mouseY - 15;
		}
 
	}
 
}
Please use plain text.
Developer
gyubok
Posts: 517
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: Moving object

THanks for the info, I've seen that code while researching. But what I am trying to simulate is an object falling down.

 

So an object is placed at the top then falls down linearly (no gravity effect).

 

I'm thinking this can be done using timer and shifting the y value one at a time with maximum of 30 fps (seems to be the suggested limit) but I don't know how to refresh an item everytime y value changes.

Please use plain text.
Developer
UberschallSamsara
Posts: 920
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Moving object

The Tweener class lets you trigger an animation with one line of code; it can probably do what you want.   There are examples at the posted link.

Please use plain text.
Developer
gyubok
Posts: 517
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: Moving object

Thanks! will look into it tonight. I'm surprise that there is no fundamental way to achieve something simple like an object move.

Please use plain text.
Developer
UberschallSamsara
Posts: 920
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Moving object

Yes there are fundamental ways to move an object but you'll have to write more code - set up a stage event listener to call a handler function to update the position based on an ENTER_FRAME event etc. like the posted code.  Tweener wraps the functionality up for you so it's just one line of code to add the animation.  It's by far not the only way to move an object but it's convenient.

Please use plain text.
Developer
tags07
Posts: 386
Registered: ‎12-12-2010
My Device: BB Style
My Carrier: Sprint

Re: Moving object

Sounds familiar :smileyhappy:  

 

This is exactly what i do in my app : https://appworld.blackberry.com/webstore/content/21601?lang=en

 

I have apples falling randomly from top.

 

I have a 

     1. Initialize bitmap:  

               [Embed(source='images/apple.png')]

private var appleClass:Class;

private var apple:Bitmap = new appleClass(); 

 

     2. I initialize the x,y coordinates of the apple

           this.apple.x=100;

           this.apple.y=0;

           mySub.addChild(this.apple);

 

 

    3. Add listener for ENTER_FRAME event

           addEventListener(Event.ENTER_FRAME, onEnterFrame);

 

    4. In the event handler i increment Y and update the apple coordinates

 

private function onEnterFrame(event:Event):void
{

    // Calculate newY

 

    this.apple.y=newY;

 

}

 

 

 

Hope that gives you some idea... you can google for onEventFrame AS3 to see more examples out there...

Please use plain text.