PDA

View Full Version : weird?


billowillo
10-31-2008, 10:39 PM
i have it so when the character gets hit it flashes red then goes back to normal, that works fine but theres an issue when another child of enemy is added. the old child wont make it turn red but the new one will, and it just continues like that whenver a new one is added the old one wont make it blink


package {


import flash.display.*;
import flash.events.*;




public class EnemyClass extends MovieClip {

private var _root:MovieClip;
private var enemyYSpeed:Number = 6;
private var enemyXSpeed:Number = 6;




public function EnemyClass() {


addEventListener(Event.ADDED, beginClass);

addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
public function beginClass(event:Event):void {
_root = MovieClip(root);

}
public function enterFrameEvents(event:Event):void {

x += enemyXSpeed;//Move the ball horizontally
y += enemyYSpeed;//Move the ball vertically
//Bouncing the ball off of the walls
if (this.x >= stage.stageWidth-this.width) {
//if the ball hits the right side
//of the screen, then bounce off
enemyXSpeed *= -1;
}
if (this.x <= 0) {
//if the ball hits the left side
//of the screen, then bounce off
enemyXSpeed *= -1;
}
if (this.y >= stage.stageHeight-this.height) {
//if the ball hits the bottom
//then bounce up
enemyYSpeed *= -1;
}
if (this.y <= 0) {
//if the ball hits the top
//then bounce down
enemyYSpeed *= -1;
}
if (hitTestObject(_root.mcChar) &&(_root.canBeHit=true)) {
trace("hit");
_root.mcChar.gotoAndStop(2)
}

if (! this.hitTestObject(_root.mcChar)){
_root.mcChar.gotoAndStop(1)
}


}

}
}

alcho
10-31-2008, 11:46 PM
Hi,

Its quite hard to tell what's going on, I'm guessing there's more code? But from looking I can see one possible flaw.

if (! this.hitTestObject(_root.mcChar)){
_root.mcChar.gotoAndStop(1)
}

This says that if the current enemy is not hitting the character, then put the character on the "not hit" frame.

Example of how this is flawed:
1)enemy 1 IS hitting character -> myChar.gotoAndStop(2)
2)enemy 2 Is not hitting character -> myChar.gotoAndStop(1)

So the last enemy to be created will ultimately have control over what frame the character will be on.

The best way to test if a character is hitting an enemy and when there is more then one enemy is to do the hit test on the characters side with all the enemies.

Eg.



var hit:Boolean = false;

for(var i =0;i<enemyArray.length;i++)
{
if(myChar.hitTest(enemyArray[i])){
hit = true
}
}

if(hit){
myChar.gotoAndStop(2);
}else{
myChar.gotoAndStop(1);
}

Hope that helps, let me know how you get on

-Alcho

billowillo
11-01-2008, 12:09 AM
that sounds logical enough, i tried plugging that in and changed all the mchar to _root.mcChar and got this error'

1120: Access of undefined property enemyArray.
for (var i =0; i<enemyArray.length; i++) {

and i really have no idea how to use that or fix it in any way.

alcho
11-01-2008, 12:25 AM
Ah sorry I wasnt more clear. You need to create the enemyArray and add all your enemies to it.

Like:

enemyArray:Array = new Array();
enemyArray.push(new enemy());
enemyArray.push(new enemy());
...

I'll need to see more of your code to understand how you are currently creating multiple enemies.

Attach the .fla if you want.

-Alcho

alcho
11-01-2008, 11:00 AM
Hi billowillo,

Thanks for sending me the fla, I understand your concerns for keeping it made private.

Ive fixed it up for you and i'll send it back but I'll also explain where you was going wrong.

I removed all the hitTests if statements from the enemy class. If you have multiple enemies, its not easy for a single enemy to be able to control the hit status of the main character as it needs to be able to check if all the other enemies are hitting him or not. It should really be on a level higher so you can see all the enemies and check them together. So back to the main timeline (frame 1).

I added the variable enemyArray to the variable list a the top to contain all the enemies.

Then whenever you want to add a new enemy (addChild(enemy)) you also add the enemy to the enemy array using push().

When you want to do the hitTests, you do a loop over all enemies in the array (which are on stage) and if ANY are hitting, set hit to true.

Finally you have your Hit value to make the decision as to whether make the character change colour, gain a point, lose a point etc.

I hope this helps, check your inbox for the version I added and good luck with your game!

-Alcho