[RESOLVED] Why does effectively the same code act differently here?
Oh great masters of actionscript, help out this very sick individual
Ok, i'm working on a platforming game, and i'll be adding enemies. To do this i thought i'd do a spawn system; a class draws rectangles on stage, if the player is detected to have came into contact with said rectangles, the corresponding wave of enemies are spawned, fairly simple right?
i'm currently using rectangle.contains to check whether character's x and y enter the area (but i've tried hittestobject/point before that). But when i spawn an enemy when this is true the simple gravity/landing-on-the-floor code (in its own 'archerenemy' class) messes up, whereas when i simply spawn an enemy in a number of other places in the the code it works.
Take a look at the attached .swf, on level start a monster drops and will remain on the floor when you move the mouse around. Move right some (with D, A for left, W for jump) and another will spawn, but this one will randomly leap about if you move the mouse. (even when the code in the archerenemy class should keep it grounded like the first!)
I've included a cut-down version of the game, the enemy spawns are all in Platformer.as. Any help is greatly, hugely appreciated!
____
In my HOURS of faffing with this i've noticed a few things that may be worth mentioning:
Removing the character reference from the rec.contains and putting anything else there will make the enemy function as normal
if you tell it to spawn the enemy if the coords and rectangle are NOT touching then the enemy works fine
if you stop Vcam moving about it seems to solve the problem; the char can jump about and the enemy will stay where its sposed too
i'll include what i'd think is the main piece of platformer.as, it may be glaringly obvious and save you guys downloading..
Code:
public function Platformer(){
LevelNo = 0
GoBtn.addEventListener(MouseEvent.CLICK, onPlay)
}
//enemy setup
private function EnemySetup(Lvl:int, x_:int){
if(Lvl == 1 && !Spawned){
Spawned = true
var enemy1:ArcherEnemy = new ArcherEnemy(x_, 0, char)
addChild(enemy1)
}
if(Lvl == 5){
var enemy:ArcherEnemy = new ArcherEnemy(x_, 0, char)
addChild(enemy)
}
}
//enterframe
private function enterFrame(e:Event){
Player.x = char.x
Player.y = char.y
// THIS MESSES UP
if((Points.rec.containsPoint(Player)) && !Spawned){
EnemySetup(1, 800)
}
// THIS WORKS, even though its effectively the same thing
/*if(!Spawned){
EnemySetup(1, 700)
}*/
//sorts out landing from jump animation (may need tweaked)
if(Jumping){
if(Floor.hitTestPoint(char.x, char.y, true)){
Jumping = false;
}
}
//move vcam
if(char.x < mouseX){
cam.x = Math.round(char.x +((mouseX - char.x) /3) )
}else if(char.x > mouseX){
cam.x = Math.round(char.x - ((char.x - mouseX) /3))
}
if(char.y < mouseY){
cam.y = Math.round(char.y +((mouseY - char.y) /3))
}else if(char.y > mouseY){
cam.y = Math.round(char.y - ((char.y - mouseY) /3))
}
//move
char.Walk(Dir)
}
This is nothing to do with your code, it's the onEnterframe that is the problem
The enterframes are not correctly synced, so the enemy that looks like it has bugged collision code is really correct, it is just lagging behind slightly. As far as I can tell, anyway. I only have Flash 8 so I can't view the fla.
To fix this, you should have only one 'onEnterframe' that is in your topmost class (or frame code, again I don't have the right version of flash so I can't see exactly how you did it) and have code inside that 'onEnterframe' that calls the functions of every child class.
So you should remove the event listener from your classes, and instead have the top class call 'onEnterframe' from its 'onEnterframe' function.
That will make sure all the onEnterframes are synced.