animating a hero with a double key press as3 Flash Pro CS6 - actionscript-3

Im creating a space shooter. Im trying to figure out how I can code/ run my movie clip (muzzle flash) when spacebar and a directional key are pressed at the same time.
This was my AS2 code using keyframes within Flash itself:
if(Key.isDown(Key.SPACE)){
this.gotoAndStop("20");
} else {
this.gotoAndStop("idle");
}
if(Key.isDown(Key.RIGHT)){
this._x += 5;
this.gotoAndStop("6");
}
if(Key.isDown(Key.SPACE)){
this.gotoAndStop("20");
}
if(Key.isDown(Key.LEFT)){
this._x -= 5;
this.gotoAndStop("6");
}
and so on...

If this was me, I would do something like this in AS3:
stop();
var velocity: Vector3D = new Vector3D(0,0,0);
var shooting: Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, function(evt: KeyboardEvent){
// have we moved on the X axis?
velocity.x = evt.keyCode == 37 ? -1: evt.keyCode == 39 ? 1: velocity.x;
// have we moved on the Y axis?
velocity.y = evt.keyCode == 40 ? -1: evt.keyCode == 38 ? 1: velocity.y;
// Have we shot?
shooting = evt.keyCode == 32 ? true : shooting;
});
stage.addEventListener(KeyboardEvent.KEY_UP, function(evt: KeyboardEvent){
// Have we finished moving on the X axis?
velocity.x = evt.keyCode == 37 || 39 ? 0 : velocity.x;
// Have we finished moving on the Y axis?
velocity.y = evt.keyCode == 40 || 38 ? 0 : velocity.y;
// have we finished shooting?
shooting = evt.keyCode == 32 ? false : shooting;
});
stage.addEventListener(Event.EXIT_FRAME, function(evt: Event){
// evaluate velocity and shooting and jump to the required keyframes.
trace(velocity, shooting);
});
The key to it is assessing which key was pressed in the two Keyboard event listeners and then at the end of the frame, then update the movieclip according to all of the data that has been gathered. I think this is important because you know that when the spaceship finally moves, it will definitely be in the most up to date state.
I also use a Vector3D to store the velocity of the spaceship as it has numerous helpful properties for calculating movement of objects such as Vector3D.scaleBy() for applying speed to the spacecraft and Vector3D.distance() for calculating the distance between the spaceship and an enemy which might be used for weapon accuracy or damage in relation to distance.

Related

How to properly code a set of walking and idle animations for a top-down isometric character for actionscript 3

I have a character for a top-down isometric game. The character is to move in eight directions, so eight idle and eight walking animations. I have the walking animations working [except for diagonal walking animations, I get a still frame for those] but if I try to add the idle animation, my walking animations get borked completely.
I'm trying to get the character to go into the idle animation based on what direction they were facing [like if they're going left, the idle will be left, if they're going diagonally up and right, then the idle will be up and right, etc.] but if I try any sort of idle code, the walk animations do not play and the idle will always be in only one direction.
Here's what I managed to get working so far:
private var speed = Number;
private var kLeft:Boolean = false;
private var kRight:Boolean = false;
private var kUp:Boolean = false;
private var kDown:Boolean = false;
//[misc code and listeners]
private function onFrame(e:Event)
{
if (kLeft == true){
this.x -= 2*speed;
this.gotoAndStop("WalkSide");
this.scaleX = -1;}
if (kRight == true){
this.x += 2*speed;
this.gotoAndStop("WalkSide");
this.scaleX = 1;}
if (kUp == true){
this.y -= 1.5*speed;
this.gotoAndStop("WalkUp");}
if (kDown == true){
this.y += 1.5*speed;
this.gotoAndStop("WalkDown");}
if (kUp == true && kLeft == true){
this.gotoAndStop("WalkUpdiagonal");
this.scaleX = -1;}
if (kUp == true && kRight == true){
this.gotoAndStop("WalkUpdiagonal");
this.scaleX = 1;}
if (kDown == true && kLeft == true){
this.gotoAndStop("WalkDowndiagonal");
this.scaleX = -1;}
if (kDown == true && kRight == true){
this.gotoAndStop("WalkDowndiagonal");
this.scaleX = 1;}
}//onFrame
private function keyPressed(k:KeyboardEvent)
{
if (k.keyCode == Keyboard.LEFT)
kLeft = true;
if (k.keyCode == Keyboard.RIGHT)
kRight = true;
if (k.keyCode == Keyboard.UP)
kUp = true;
if (k.keyCode == Keyboard.DOWN)
kDown = true;
}//keypressed
private function keyReleased(k:KeyboardEvent)
{
if (k.keyCode == Keyboard.LEFT)
kLeft = false;
if (k.keyCode == Keyboard.RIGHT)
kRight = false;
if (k.keyCode == Keyboard.UP)
kUp = false;
if (k.keyCode == Keyboard.DOWN)
kDown = false;
}//keyreleased
So how do I add in coding that if I release [kLeft] then "IdleSide" is visible, etc?
Here is my current .swf file.
Given your current code, one way you can try would be to separate out your direction handling and walking/idle states.
Right now, it looks like you're doing this:
You set a boolean upon key press and on enter frame you check if that boolean is true, then you walk in that direction.
Instead, you can do this:
Have a set of direction states (booleans in your case) and character states (two booleans for now, one for walking, one for idle)
Upon key press, you set both the direction state and character state (say, from idle to walking + left).
Upon key release, you only update the character state and leave the direction state alone (from walking to idle).
Then, in your enter frame loop, you can look up both the direction and character state as to which animation to play, which would be idle + left.
In the future, you may run in to an issue of having tons of flags/booleans if you decide to implement more character states (say.. "resting", or "running"). It might be worth it at this point to do some research in to other ways to handle this problem.
I would suggest you to rewrite your code in a more legible and organized way. The best way to achieve this is something like this:
first of all, creat a movie clip container for each direction's animation, in this example I will only use 4 directions, so we would have:
leftAnimations_mc
rightAnimations_mc
upAnimations_mc
downAnimations_mc
on each frame separate each animation in one of this ways:
1.- each animation in diferent layers, one animation after another, labeling each animation's start keyframe such as "idle", "walk", "run" and so on.
2.- each animation inside a movie clip ("leftIdle_mc", "leftWalk_mc", etc) and add each animation in a different keyframe in your main animations movie clip: "leftAnimations_mc", and label each frame the same way as the previous eample: "idle, walk, run". (I prefer this way because in case you need to update your animation (making it longer or shorter) its easier just tu update the frame in your inner leftIdle_mc or leftWalk_mc, and having the main leftAnimations_mc (which holds all the other animations) intact.
3.- create a new MovieClip called player_mc, and add each animation container in different layers of this movieClip and set an instance name for each animation movieclip.
in the end you should have something like this:
player_mc -> leftAnimation_mc -> "idle" keyframe -> leftIdle_mc
-> "walk" keyframe -> leftWalk_mc
-> rightAnimation_mc-> "idle" keyframe -> rightIdle_mc
(you got the idea...)
now for the coding:
1.- setup your directions and animations like constants, declare a variable for the current direction and declare a variable for the current animation:
private const LEFT:string = "left";
private const RIGHT:string = "right";
private const UP:string = "up";
private const DOWN:string = "down";
private const IDLE:string = "idle";
private const WALK:string = "walk";
private var currentDirection:string = "right";
private var animation:string = "idle";
2.- add your keyDown and keyUp listeners.
in your keyDown event listener, instead of setting true or false values, set your current direction depending on the key pressed, add the animation you want to call and finally add a new method called updateAnimations() e.g.:
if(k.KeyCode == KeyBoardLeft)
{
currentDirection = LEFT;
animation = WALK;
updateAnimations();
}
3.- on your key up/released event, just change your animation to IDLE and also add the updateAnimations method:
private function keyReleased(k:KeyboardEvent)
{
animation = IDLE;
updateAnimations();
}
4.- finally the updateAnimations and hideAnimations methods:
private updateAnimations()
{
hideAnimations();
switch(currentDirection)
{
case LEFT:
player_mc.leftAnimations_mc.visible = true;
player_mc.leftAnimations_mc.gotoAndStop(animation);
break;
case RIGHT:
player_mc.rightAnimations_mc.visible = true;
player_mc.rightAnimations_mc.gotoAndStop(animation);
break;
}
}
private hideAnimations()
{
player_mc.leftAnimations_mc.visible = false;
player_mc.rightAnimations_mc.visible = false;
player_mc.upAnimations_mc.visible = false;
player_mc.downAnimations_mc.visible = false;
}
as you can see, I've used hideAnimations() to hide all the animations movie clips, and then with a switch I set visible only the animation I want, finally I just goto the current animation key frame:
player_mc.rightAnimations_mc.gotoAndStop(animation);
Hope this is clear enough.

Why doesn't my KEY_DOWN event fire?

I am new to ActionScript and was following the tutorial here to get me started using FlashDevelop. I set it up to move the item every KEY_DOWN event instead of using the frame event but my KEY_DOWN event is never fired. I quickly added a MOUSE_DOWN event to test the extent of the problem which works.
I am adding the listeners as my Paddle (an extension of Sprite) item is added to the stage:
private function addedToStage(e:Event) : void {
// Remove this event listener
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
// Add the picture
addChild(pic);
// Set the location of the item in the middle of the screen one
// height length of the image down.
this.y = this.height;
this.x = (this.stage.stageWidth - this.width) / 2;
// Add the keyboard listener (broken).
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDownHandler, false, 0, true);
// Add the mouse listener (works).
stage.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDown, false, 0, true);
// Set the focus to the stage.
stage.focus = stage;
}
I have a breakpoint in my keyDownHandler function and it is never called.
This may be a duplicate of this question, but I am already doing what the answer in that question describes and in the comments he explains that he does not know what fixed the problem only that it suddenly started working.
Another popular answer to that question mentions this article which is why there is a line in my code to set the focus back to the stage. There is nothing else on my stage.
What could be happening?
Edit:
Here is my listener code:
private function keyDownHandler(e:KeyboardEvent):void {
var increment:Number; // Here is my breakpoint
if (e.keyCode == 65 || e.keyCode == 37) {
increment = -5;
} else if(e.keyCode == 68 || e.keyCode == 39) {
increment = 5;
}
var newX:Number = this.x + 5;
if (newX > 0 && newX + this.width < this.stage.width) {
this.x = newX;
}
}
The problem was unrelated to the event itself, but rather how I was testing to see if it fired. You cannot place breakpoints on declarations.
FlashDevelop will allow you to set a breakpoint there but the debugger will not actually stop at this point.
The code to move the paddle does not work (not surprising to me at this point), but if I move the breakpoint to the line in the if statement (or any other line when an actual action performed such as the trace) suddenly it breaks fine. Now that I think about it even the other languages I've worked with have this behavior, I'm just too pampered by IDE's that will move the breakpoint to the nearest available line in such situations.

AS3 Collision detection or Keyboard Control altering my player MovieClip path

Im making a maze-like game where the player controls a movieclip (_character) around the stage with the arrows or WASD and collects things in a certain amount of time. The MovieClip has five different frames, each with their own state animations, walk Left, right, up, down and stationary.
On the stage there are squares/boxes with collision detection and I need to be able to manoeuvre around them in all different directions.
My problem is that I cant seem to get the animations inside the movieclip AND correct movement of the movieclip itself working at the same time.
My current controls can zip around the obstacles great, stops when no keys are pressed and plays relevant _character animations, but when two diagonal direction keys are pressed the MovieClip travels diagonally, even after I release one of the keys.
e.g If there is one square/box on the stage, which I want to travel clockwise around it (starting at top side) I should be able to press right, down, left, up, then right once more to return to my original position.
However with my current code I'll press right, down (and before I can press left to go around the box) my Movieclip will travel to the lower right corner of the stage (down+right).
Even if I take my finger off the DOWN key and just press RIGHT it wont correct itself and go right, its stuck in that diagonal position!
Image gives example of pressing LEFT, then DOWN as game starts.
When I remove all the boxes from the stage to test movement in an empty area, the movieclip object still becomes fixed to a diagonal direction if I push down on two buttons, with no boxes to move around it just travels to the corner of the stage it is aimed at.
Preventing this is my problem, I cant just disable multiple key presses because then it becomes too difficult to move around the maze (_character would have to be EXACTLY lined up to the gap)
Current Code:
Variables for directions
private var _bUp:Boolean = false;
private var _bDown:Boolean = false;
private var _bLeft:Boolean = false;
private var _bRight:Boolean = false;
Variables true in KeyDown Handler.
private function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == 65 )
{
_bLeft=true;
}
else if (event.keyCode == Keyboard.RIGHT || event.keyCode == 68)
{
_bRight=true;
}
else if (event.keyCode == Keyboard.UP || event.keyCode == 87 )
{
_bUp=true;
}
else if (event.keyCode == Keyboard.DOWN || event.keyCode == 83)
{
_bDown=true;
}
False in KeyUp.
private function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT
|| event.keyCode == 65 || event.keyCode == 68)
{
_bLeft=false;
_bRight=false;
}
else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP
|| event.keyCode == 87 || event.keyCode == 83 )
{
_bUp=false;
_bDown=false;
}
In the enterFrameHandler _character velocity is moved and animations play. If no keys pressed, velocity is 0 and _character is still. Then character movieclip gets the value of velocity to move, we check stage boundaries and then collisions with the boxes.
private function enterFrameHandler(event:Event):void
{
var _updown:Boolean=Boolean(!(_bUp==_bDown));
var _leftright:Boolean=Boolean(!(_bLeft==_bRight));
if (!_updown && !_leftright)
{
// not moving anywhere
_character.gotoAndStop(1);
_character.vy=0;
_character.vx=0;
}
else
{
if (_bUp)
{
_character.vy=-4;
_character.gotoAndStop(2);
}
else if (_bDown)
{
_character.vy=4;
_character.gotoAndStop(3);
}
if (_bLeft)
{
_character.vx=-4;
_character.gotoAndStop(4);
}
else if (_bRight)
{
_character.vx=4;
_character.gotoAndStop(5);
}
}
_character.x += _character.vx;
_character.y += _character.vy;
checkStageBoundaries(_character);
//Collisions
Collision.block(_character,_box1);
Collision.block(_character,_box2);
collisions with the boxes.^^^
On a side note
Not sure if its of significance but my older keyboard method only used the enterFrame Handler to link the velocity to the character object and the KeyDown Handler moved the chacters velocity. This used the same collision detection boxes but I had no problems moving the movieclip object
That code was:
private function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == 65 )
{
_character.vx = -4;
_character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT || event.keyCode == 68)
{
_character.vx = 4;
_character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP || event.keyCode == 87 )
{
_character.vy = -4;
_character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN || event.keyCode == 83)
{
_character.vy = 4;
_character.gotoAndStop(3);
}
}
// Key up handler stops velocity and plays first frame which is the stationary image.
private function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT
|| event.keyCode == 65 || event.keyCode == 68)
{
_character.vx = 0;
_character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP
|| event.keyCode == 87 || event.keyCode == 83 )
{
_character.vy = 0;
_character.gotoAndStop(1);
}
}
This movement technique moved the MovieClip around the stage perfectly, no strange routes, but the animations wouldnt play properly with multiple key presses, so I switched to the boolean variables and enterFrame control.
In Conclusion
I have no idea if the collision detection or keyboard controls are to blame, but I would just like to be able to manoeuvre the player Movieclip quickly around the boxes and have the appropriate state animations play.
Ive seen tile based design mentioned before, but I can get what I want in the two different keyboard attempts above. Surely I can get the best of both worlds without redesigning the whole thing as tiles?! Help me please!
Got it. I didn't tell you that _character's velocity should be cleared before parsing current movement. Sorry. So, you add this:
_character.vx=0;
_character.vy=0;
into the function enterFrameHandler at its start, and this should make your char go proper way.

AS3 Keyboard Controlled Character - Animation problems with multiple key presses

I have a keyboard controlled character in a game which has a stationary frame when no
keys are pressed and four different animations for the up, down, left, right directions
(No diagonal movement).
So when left key is pressed, _character moves left across the screen and the left animation is played. When the key is released/Key up the stationary frame plays where the character is still.
This works fine at a slow pace but when multiple keys are being pressed to maneuver around an obstacle etc, the character often sticks to the stationary position and only plays the animations after a delay. The movement of the Movieclip is fine using velocity, but the animations suffer.
Its something to do with the key up handler surely. I’m a beginner so I’m sure I just can’t get my head around some simple logic, but I can’t figure it out! Help me please. No matter how fast I tap the keys I need that animation to play, and only when nothing is being pressed should I see the stationary image.
Character in its own class, with Velocity variables.
package
{
import flash.display.MovieClip;
import flash.display.DisplayObject
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
public class Character extends MovieClip
{
//Public properties
public var vx:int = 0;
public var vy:int = 0;
public function Character()
{
}
}
}
Keydown and KeyUp listeners added.
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
In enterFrame the characters X and Y position is linked to the velocity
private function enterFrameHandler(event:Event):void
{
//Move the game character and check its stage boundaries
_character.x += _character.vx;
_character.y += _character.vy;
}
The keydown handler uses the arrows and WASD to control the velocity and plays that directions specific frame (with embedded animation)
private function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == 65 )
{
_character.vx = -4;
_character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT || event.keyCode == 68)
{
_character.vx = 4;
_character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP || event.keyCode == 87 )
{
_character.vy = -4;
_character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN || event.keyCode == 83)
{
_character.vy = 4;
_character.gotoAndStop(3);
}
}
Key up handler stops velocity and plays first frame which is the stationary image.
private function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT
|| event.keyCode == 65 || event.keyCode == 68)
{
_character.vx = 0;
_character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP
|| event.keyCode == 87 || event.keyCode == 83 )
{
_character.vy = 0;
_character.gotoAndStop(1);
}
}
I also made the stationary image bright red to make it easier for me to spot and once I start moving the character in different directions I see Red! (literally & figuratively!)
Update
Vesper's answer has solved the animation state problem brilliantly but this new control system seems to want to travel diagonally which sends the character on an unwanted path.
My stage is a grid of 50px squares. Character is 50px and 50px boxess (with collision detection) are around with a 50px border around them so the character can maneuver around the stage.
If I press/hold one direction the character moves no problem (left to right and Up to down is fine too.), but if I go left then down or up then right etc, the character will travel that direction then continue that direction around the boxes. When I tested without the boxes the character continues in that direction diagonally.
So if I want to go clockwise around a box (starting in the bottom left side of it), I'll press up, then right and if I dont press down VERY quickly, the character will travel up and right again, towards the top right of the stage. (Character does stop if key is released.)
So if I press two keys from the opposing updown or leftright variables, it wants to travel diagonally. Any ideas how to solve this?
Final Update
Vesper's answer solved my original animation problem. I'll ask new questions to deal with remaining queries.
You actually have four de-facto independent key states, up key pressed/released, down key, left key, right key. Technically one can implement special actions in case of two opposite keys be pressed at one time, in your case it's not needed but it's still worth mentioning. So, you handle all of them separately, and only then choose where should your character face.
var bDown:Boolean=false;
var bUp:Boolean=false;
var bLeft:Boolean=false;
var bRight:Boolean=false;
// declare vars to store key states. You can use an array if you want
private function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == 65 )
{
bLeft=true;
// now, instead of directly attaching the movement, just set flag
}
else if (event.keyCode == Keyboard.RIGHT || event.keyCode == 68)
{
bRight=true;
}
else if (event.keyCode == Keyboard.UP || event.keyCode == 87 )
{
bUp=true;
}
else if (event.keyCode == Keyboard.DOWN || event.keyCode == 83)
{
bDown=true;
}
}
Same for key up handler, setting values to false. Now doing the parsing of the actual character's movement according to keys pressed. The best place is the enter frame handler - good thing you have it in place already.
private function enterFrameHandler(event:Event):void
{
var updown:Boolean=Boolean(!(bUp==bDown));
// if both are true or both are false, this is false. If this is true,
// we are moving upwards or downwards
var leftright:Boolean=Boolean(!(bLeft==bRight));
// same here
if (!updown && !leftright) {
// not moving anywhere
_character.gotoAndStop(1);
_character.vy=0;
_character.vx=0;
// other mechanics might be in place in case you decide to implement inertia, for example
} else {
if (bUp) {
_character.vy=-4; // hardcoding this might get you issues later
// will still do for today's task
_character.gotoAndStop(2);
} else if (bDown) {
_character.vy=4;
_character.gotoAndStop(3);
}
// one side parsed. Note, with this sequence the leftwards or rightwards
//animation supersedes up/down. But, we don't have diagonals, so there should be superseded animation
if (bLeft) {
_character.vx=-4;
_character.gotoAndStop(4);
} else if (bRight) {
_character.vx=4;
_character.gotoAndStop(5);
}
}
// Okay, now velocity and facing is set, proceed with move
//Move the game character and check its stage boundaries
_character.x += _character.vx;
_character.y += _character.vy;
}

Creating collision detection on more than one variable in a for loop?

Essentially I am trying to create a game, where the player has to dodge certain items, so far I have a piece of code that randomly adds 3 sharks to the stage.
The idea is that once the player has hit a shark he/she returns to the start location, I have an Action Script file that contains the speed,velocity etc of the shark, the every time the program is run the sharks will appear in a different location.
However, when I attempt to do a collision test of the sharks only one of the sharks respond, I cannot figure out how to make it that all 3 sharks effect the player (square_mc). Any help would be greatly appreciated.
//Pirate game, where you have to avoid particular object and get to the finish line to move onto the final level.
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveMode );
function moveMode(e:KeyboardEvent):void {
//movements for the pirate ship, this will allow the ship to move up,down,left and right.
if (e.keyCode == Keyboard.RIGHT) {
trace("right");
square_mc.x = square_mc.x + 25;
}
else if (e.keyCode == Keyboard.LEFT) {
trace("left");
square_mc.x = square_mc.x - 25;
}
else if (e.keyCode == Keyboard.UP) {
trace("up");
square_mc.y = square_mc.y - 25;
}
else if (e.keyCode == Keyboard.DOWN) {
trace("down");
square_mc.y = square_mc.y + 25;
}
}
//for.fla
//this program uses a for loop to create my Sharks
//a second for loop displays the property values of the sharks
function DisplayShark():void{
for (var i:Number=0;i<3;i++)
{
var shark:Shark = new Shark(500);
addChild(shark);
shark.name=("shark"+i);
shark.x=450*Math.random();
shark.y=350*Math.random();
}
}
DisplayShark();
for(var i=0; i<3;i++){
var currentShark:DisplayObject=getChildByName("shark"+i);
trace(currentShark.name+"has an x position of"+currentShark.x+"and a y position of"+currentShark.y);
}
//here we will look for colliosion detection between the two move clips.
addEventListener(Event.ENTER_FRAME, checkForCollision);
function checkForCollision(e:Event):void {
if (square_mc.hitTestObject(currentShark))
{
trace("The Square has hit the circle");
square_mc.x=50
square_mc.y=50 //these lines of code return the square back to it's original location
}
}
Just move your for loop into the ENTER_FRAME:
addEventListener(Event.ENTER_FRAME, checkForCollision);
function checkForCollision(e:Event):void {
for(var i=0; i<3;i++){
var currentShark:DisplayObject=getChildByName("shark"+i);
if (square_mc.hitTestObject(currentShark))
{
trace("The Square has hit the circle");
square_mc.x=50;
square_mc.y=50;
}
}
}
You can't just go through the for loop once and set the currentShark variable - you'll just end up testing against one shark every time you perform the collision test. Rather, every time you want to check collisions you must loop through all the sharks and do the collision testing.