Hey guys…
Once again a bad title ^.^
So this is what im trying to accomplish:
Their is two buttons on the stage… one says “Spawn” and the other says “Attack”. When you click on the spawn button it will create a moveclip and a child and add it to an array… say for example you create three movieclips… You click on the one you want selected then you can control him, clicking on the attack button will make the little movieclip “Attack” and it will move to wherever you click on the stage (X Axis only). Both of these functions work except their is a little glitch in the moving. If one of the movieclips is moving and you “select” a different one, the one moving will stop moving… I do know why it does this, just not how to fix
Here is the code:
Variables \\
var CurrentRifle:RifleSquad; // Variable for to see which Rifle Squad is currently Selected
var rifles:Array = [] // The array which stores each Rifle Squad so they can be reffered to later on
var CharSpeed:Number = 1 // The speed at which the Characters will move at
var selectedX; // The variable for storing the mouses X when the stage is clicked
// Event Listeners \\
SpawnRiflemenBtn.addEventListener(MouseEvent.MOUSE_DOWN, AddRifleMenHandler); // Handles the button press for spawning rifle men
SquadAttackBtn.addEventListener(MouseEvent.MOUSE_DOWN, SquadAttackHandler); // Handles the button press for attacking
Move.addEventListener(MouseEvent.MOUSE_DOWN, MouseXPos); // Event Listener for getting the Mouse's X Pos
// Handles the spawning of "Riflemen"
function AddRifleMenHandler(event:MouseEvent):void {
var newRifle:RifleSquad = new RifleSquad() // RifleSquad being the "Linkage" name of the movieclip in question
var container:MovieClip = new MovieClip()
container.addChild(newRifle)
addChild(container)
rifles.push(newRifle)
container.x = 200
container.y = Math.ceil(Math.random()*150)
newRifle.addEventListener(MouseEvent.MOUSE_DOWN, RifleMenSelectHandler); // Event Listener for the selection of people
}
// Sets the variable for the currently selected Rifle Squad to the currently selected Rifle Squad ^.^
function RifleMenSelectHandler (e:MouseEvent)
{
CurrentRifle = RifleSquad(e.target)
selectedX = CurrentRifle.x
trace(e.target)
Move.addEventListener(Event.ENTER_FRAME, moveCharacter); // Event Listener for moving the character
}
// Handles the attacking for Riflemen
function SquadAttackHandler (e:MouseEvent)
{
CurrentRifle.gotoAndStop(2);
}
// Function for getting the mouse's X
function MouseXPos (e:MouseEvent)
{
selectedX = stage.mouseX - 200
}
// Function for moving the character
function moveCharacter(event:Event) {
if (CurrentRifle.x > selectedX) {
CurrentRifle.x = Math.max(CurrentRifle.x - CharSpeed, selectedX) // Make the character move right
}
else if (CurrentRifle.x < selectedX){
CurrentRifle.x = Math.min(CurrentRifle.x + CharSpeed, selectedX) // Make the character move left
}
}
The problem is is that when you “Select” another movieclip (RifleSquad) it changes the “CurrentRifle” to that instance… therefore that code that makes them move doesnt apply to them anymore… I really need help with this as I need to be able to make lots of them move at the same time…
Thanks Heaps
-Kaine