PDA

View Full Version : [AS3] Teleportation


nonprogrammer
04-29-2009, 09:56 PM
The end result should be as follows:
There will be 5 doors and one enemy. The doors will be on the stage, while the enemy will be added to the stage via code. The doors will all have an initial state of being closed. After a small amount of time I will change the state of all those doors to being open. When this state change happens, I want to spawn my single enemy in one of those doors at random. This enemy will then move around the stage (flying) based upon specific paths I set out (multiple paths, one chosen at random) and end their trip by flying back into one of the doors and be removed from the stage, with the doors going back to their closed state.
Rinse and repeat.

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;

var doorClosed:Boolean = true

// Create a new Timer object with a delay of 10 seconds
var myTimer:Timer = new Timer(10000);
myTimer.addEventListener("timer", timerFunction);

// Start the timer
myTimer.start();
trace("Doors are closed");


// Function will be called every 10 seconds
function timerFunction(event:TimerEvent)
{
if (doorClosed = true)
{
Door.gotoAndPlay(1);
doorClosed = false;
trace("Doors are open");
}

else
{
Door.gotoAndPlay(2)
doorClosed = true
trace("Doors are closed");
}
}

Now I've got 2 issues with this:

1) Even though all 5 doors were made by dragging out multiple copies of a single movie clip and all of them have the same instance name of "Door", only 1 of them is being affected by the code. What's going on?

2) This one isn't a big deal, but I want to know how to fix it anyway. Watching the file play shows the door changing from green (closed) to blue (open) to green (closed) and so forth just like it is supposed to. The issue is that I don't have the traces properly placed, so it traces "The doors are closed." and then "The doors are open." and then keeps on tracing "The doors are open." every single time, even when they switch to closed.

Any help in making all the code more neat/efficient would also be great.

One more thing, someone else (on another site) told me the following about how to draw the enemy to the screen (and then never clarified or helped again):

5) when the time is as long as you want, or when the doors are open,

_root.createMovieClip("[name]", "[name]", 100);

Then give it x and y coordinates of the locations or equal to the doors. Use "if" statements with a "random(#)" for which door to create it at. Happy to help with that, too.

I'm not actually sure what he was saying to me since I'm new to this.

Any help will be greatly appreciated.

scarce
04-29-2009, 11:25 PM
The end result should be as follows:
There will be 5 doors and one enemy. The doors will be on the stage, while the enemy will be added to the stage via code. The doors will all have an initial state of being closed. After a small amount of time I will change the state of all those doors to being open. When this state change happens, I want to spawn my single enemy in one of those doors at random. This enemy will then move around the stage (flying) based upon specific paths I set out (multiple paths, one chosen at random) and end their trip by flying back into one of the doors and be removed from the stage, with the doors going back to their closed state.
Rinse and repeat.

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;

var doorClosed:Boolean = true

// Create a new Timer object with a delay of 10 seconds
var myTimer:Timer = new Timer(10000);
myTimer.addEventListener("timer", timerFunction);

// Start the timer
myTimer.start();
trace("Doors are closed");


// Function will be called every 10 seconds
function timerFunction(event:TimerEvent)
{
if (doorClosed = true)
{
Door.gotoAndPlay(1);
doorClosed = false;
trace("Doors are open");
}

else
{
Door.gotoAndPlay(2)
doorClosed = true
trace("Doors are closed");
}
}

Now I've got 2 issues with this:

1) Even though all 5 doors were made by dragging out multiple copies of a single movie clip and all of them have the same instance name of "Door", only 1 of them is being affected by the code. What's going on?

2) This one isn't a big deal, but I want to know how to fix it anyway. Watching the file play shows the door changing from green (closed) to blue (open) to green (closed) and so forth just like it is supposed to. The issue is that I don't have the traces properly placed, so it traces "The doors are closed." and then "The doors are open." and then keeps on tracing "The doors are open." every single time, even when they switch to closed.

Any help in making all the code more neat/efficient would also be great.

One more thing, someone else (on another site) told me the following about how to draw the enemy to the screen (and then never clarified or helped again):



I'm not actually sure what he was saying to me since I'm new to this.

Any help will be greatly appreciated.

Ok, first off, you can never have multiple movieclips with the exact same name, then it will only effect the first found object, you gotta do something like: "door1", "door2", etc. Then run a for/loop to affect all objects you want I.e:

DoorCount=0
for(I=0;I<=doorCount;I++){
doorCount++
Var Doors = eval("door"+doorCount)
//all door coding in here using "doors"
}

this you'll have to implement into as3 coding, cause this is as2 code, might be the same, I dunno.

As far as attaching the movie to the stage, you need to go into your library, right click/apple click the enemy mc, go to linkage, check the "export to actionscript" box, then put the name you want it I.e: enemy.

THEN you can attach it using:

attachMovieClip("enemy", "enemy", getNextHighestDepth())


The first "enemy" is the mc name you set, the second "enemy" is where you can rename it else leave the same.

nonprogrammer
04-30-2009, 03:05 AM
What I don't get is why I even have to differentiate between the multiple doors. They are all just instances of a single movie clip, so should I not be able to effect them all without a loop? Isn't it possible for it to affect the movie clip and therefore have all instances of it effected?

I know how to export for actionscript, already have it done, but thanks anyway.

I was just wondering what the difference is between what you gave me:
attachMovieClip("enemy", "enemy", getNextHighestDepth())

and this:

_root.createMovieClip("[name]", "[name]", 100);

Basically, why attachMovieClip instead of _root.createMovieClip? And what is getNextHighestDepth?

rrh
04-30-2009, 04:26 AM
I don't think createMovieClip is a real function. attachMovieClip and createEmptyMovieClip are real functions, and he might have got them confused a little. But both of them are deprecated if you are using AS3, in favour of something more like this:

var doors:Array =[];
var frank:enemy = new enemy();
addChild(frank);
doors.push(frank);


A lot of these things if you type the word into the actionscript in your Flash document, highlight the word, and press F1, you'll get a more detailed explanation than someone as lazy as me will bother giving you.