PDA

View Full Version : [AS2] swapping instances (character for game)


orien2v2
09-08-2009, 02:35 PM
Hey guys I'm new to Flash and just learned some basic scripting.
Now I'm trying to do some animation which involves a character walking to a certain point then stops there.

I have created two movieclips, 1 is the animated character walking with instance name "char1", another is an animated character standing still but breathing with instance name "char2"

what I want is the char1 move along the x axis until (for example, x=600, the stage size is 800x600) and when it reaches 600, it stops and the instance (or symbol) changes to char2.

Simply put it, i want the character to walk, then comes to a halt, standing still at 600.

what I've done so far is this:

_root.char1.onEnterFrame=function(){
_root.char1._x+=2
}

that's what i did to get it walking. I've already placed the instance on the stage by dragging from the library. Thx a bunch!

asf8
09-08-2009, 05:07 PM
Welcome to Actionscript.org orien2v2 ;)

Instead of using two movieclips for walking/standing use a single movieclip and place your two animated movieclips inside this single movieclip which you will place on stage with an instance name of char1. Inside this char1 movieclip, Frame 1 is the walking animation, Frame 2 is your standing still animation. Then you can use a conditional (if statement) to run a check for when the character gets to the destination (600), when it does switch it from the walking frame (frame 1) to the standing frame (frame 2) and remove the onEnterFrame so it stops (at 600).

Like so, for example....


// Code goes in main timeline of your FLA in an actions layer
var speedNum = 2;
var stopX = 600;
char1.gotoAndStop(1);
char1.onEnterFrame = function() {
char1._x += speedNum;
if (char1._x>=stopX-char1._width) {
delete char1.onEnterFrame;
char1.gotoAndStop(2);
}
};

Maybe that will get you going, hope it helps.

orien2v2
09-09-2009, 05:09 PM
wow! thx asf8 I never thought of just changing the frames with different mc's in it! U're a genius haha. But is there any code that can swap instances in actionscript??? Just curious to know :D

asf8
09-09-2009, 07:50 PM
wow! thx asf8 I never thought of just changing the frames with different mc's in it! U're a genius haha. But is there any code that can swap instances in actionscript??? Just curious to know

Well you could use attachMovie and "export for actionscript" on your two Movieclips in your library.

So then it would be something like this... (using linkage identifiers of standing / walking respectively for the 2 MovieClips).

var speedNum = 2;
var stopX = 600;
var depthNum:Number = this.getNextHighestDepth();
var char1:MovieClip = this.createEmptyMovieClip("char1", depthNum);
var walkMC:MovieClip = char1.attachMovie("walking", "walkMC", 1);
var standMC:MovieClip;
char1._x = 0;
char1._y = Stage.height/2;
char1.onEnterFrame = function() {
char1._x += speedNum;
if (char1._x>=stopX-char1._width) {
delete char1.onEnterFrame;
standMC = char1.attachMovie("standing", "standMC", 1);
}
};

Hope it helps. ;)