|
-
attachMovie and keypresses
hi
I'm making a piece where i have a character moving and i want it to leave a trail of dots to show where it has been.
I created a movie clip with the dot in it, and i have been trying to use the 'attachMovie' command, so that the position of the dot matches that of the characters x and y movement.
i have the code to move the character on the character movie clip (instance name = char)
then i have this code on the first frame of the timeline:
for(var i:Number = 0; i < 100; i++)
var crumb:MovieClip = attachMovie("crumb", "crumb" + i, i);
crumb._x = char._x;
crumb._y = char._y;
where crumb is the movieclip i want to attach.
All that happens is that flash creates on dot (crumb) in the same position each time, not determined by the character position.
Any help much appreciated. I realise I am missing out steps but i'm not sure where to go from here! thanks!
-
PHP Code:
for(var i:Number = 0; i < 20; i++){ attachMovie("crumb", ["crumb"+i],i, {_x:char._x, _y:char._y}); }
try this
-
hey
thanks for that. i have just tried it, and it now attachs the crumb movie to the co-ordinates of the character and moves with it so it's half-way there.
what i would like, is for the crumbs to stay on the stage to show that the character has been there, like making a trail. so the next stage is to get the crumbs generated to stay while the character moves along. i hope this makes sense!!
-
hmm. this script doesn't delete the attached mc. They must be there, where they are attached.
-
Creephun, a loop won't help. All a loop will do is attach 20 "crumbs" to the stage at once.
tealady - I don't know what code you're using for the character but I've written a little code from scratch since you didn't post an FLA.
Here's what you'll need to do..
Assuming the crumb MC has a linkage identifier of "crumb", and your character has instance name "man". Copy this code into the main timeline:
PHP Code:
var walkSpeed = 3; var crumbCount = 0; man.onEnterFrame = function() { if (Key.isDown(Key.LEFT)) { man._x -= walkSpeed; } else if (Key.isDown(Key.RIGHT)) { man._x += walkSpeed; } else if (Key.isDown(Key.UP)) { man._y -= walkSpeed; } else if (Key.isDown(Key.DOWN)) { man._y += walkSpeed; } else { } }; function addCrumb() { crumbCount++; attachMovie("crumb", "crumb"+crumbCount, crumbCount); _root["crumb"+crumbCount]._x = man._x; _root["crumb"+crumbCount]._y = man._y; _root["crumb"+crumbCount].swapDepths(man); } var crumbInt=setInterval(addCrumb,500);
Make sure that you use
PHP Code:
clearInterval(crumbInt);
when you move to another section of your game or where you don't want to leave a crumb trail.
Basically - the function attaches a crumb MC to the stage and sets it _x,_y properties to the same as the current position of the character. It then puts that crumb behind the character using the swapDepths() method. The depth thing is purely for aesthetics though.
Hope that helps you!
Last edited by lightspeed10; 11-29-2009 at 02:10 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|