PDA

View Full Version : Moving Sprites on the x-axis



jeancnicolas
May 19th, 2009, 07:34 AM
Hi I am converting at last to as3 ..

for ease, I am using tweener to move the objects.
I have randomly generated a load of Sprites, inside a container, along the (x,y,z) axis.

I want to scroll the objects along the x-axis when I move the mouse left and right.
function Scroll():void
{
cont = new Sprite();
cont.x = stage.stageWidth * 0.5;
cont.y = stage.stageHeight * 0.5;
addChild(cont);
makeChildren();
addEventListener(MouseEvent.MOUSE_MOVE, onMove); // this is what worries me! - the placing of this line of code
}
function makeChildren():void
for(var i:uint = 100; i>0; i--)
{
var sp:Sprite = new Sprite();
sp.graphics.beginFill(Math.random()*0xFFFFFF);
sp.graphics.drawRect(0,0,300,300);
sp.x = Math.random()*1000-500;
sp.y = Math.random()*1000-500;
sp.z = i*200;
cont.addChild(sp);
}
onMove(e:MouseEvent):void
{
var sp:Sprite = Sprite(e.target);
Tweener.addTween(cont,{x:mouseX,time:1}); // simply moving the objects to mouseX
}
Great - Hmm - but the objs only move when the mouse rolls Over them. Why is this? I want them to move whenever the mouse does ...

Can anyone help!?

kadaj
May 19th, 2009, 08:40 AM
var cont:Sprite = new Sprite();
cont.x = stage.stageWidth * 0.5;
cont.y = stage.stageHeight * 0.5;
addChild(cont);
makeChildren();
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);

function makeChildren():void
{
for(var i:uint = 50; i>0; i--) {
var sp:Sprite = new Sprite();
sp.graphics.beginFill(Math.random()*0xFFFFFF);
sp.graphics.drawRect(0,0,100,100);
sp.x = Math.random()*1000-500;
sp.y = Math.random()*1000-500;
sp.z = i*200;
cont.addChild(sp); // cont is inside the function scroll.
}
}

function onMove(e:MouseEvent):void
{
cont.x = mouseX; //moves the container along x-axis depending on mouse movement

}I think this is what you wanted.
cont is inside the function scroll. so calling cont.addChild(sp) isn't possible.

jeancnicolas
May 19th, 2009, 10:11 AM
Yes it works.
It's also about getting my head around when adding a listener, either with:
addEventlistener()
this.addEventListener()
cont.addEventListener()
& as you put stage.addEventListener()
...see they all seem to work, and its knowing which one to use in the right situation.

I also realised I needed to create a 'loop' enterframe function, so that I could then add some smooth movement to it all via += friction.

ta ../