For class I had to make an as3 top down view game.
Everything seems to work fine in level1 but there is a sound delay on level2. When the character collides with the hand in level2 and the sound object is called to play, the sound does not play instantly, it delays until the collision is not true, and also only plays if the character has not moved from where the collision occured.
Currently the character runs through level 1 while properly making sound play on collision with its .cc_mc collider object and the hand's hc_mc collider object... but on level2 character collides with the new hand and sound doesn't play until level2.hand.grabbing_mc disappears. The timed hand first sways with an argument to go to the sway instance, then on to the grab instance labelled "grabbing_mc". the hc_mc collider object is within the grabbing_mc. Weirdly, the scythe scream sound works fine through the whole game without lag.
Below I have a stripped version of my screengame class which does most of the game work; mostly just parts related to sound. anyone know how to remove the delay on level 2 after collision? Thank you in advance.
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.media.*;
import flash.net.URLRequest;
public class ScreenGame extends Screen
{
public var character:Character;// Uses the AS Linkage
public var handScream:Sound = new Sound(new URLRequest("handScream.mp3"));
public var channel2:SoundChannel;
public var handNow:Boolean = false;
// Declare a level1 object to use in the game
public var level1:Level1;
public var level2:Level2;
//out of time timer
private var OTimer1:Timer;
private var OTimer2:Timer;
public function ScreenGame()
{
addEventListener(Event.ADDED_TO_STAGE,onStage);
}
public function onStage(e:Event)
{
removeEventListener(Event.ADDED_TO_STAGE,onStage);
// Creates character object
// character can now be used, which means positioning, animating, etc...
character = new Character(stage.stageWidth * 0.04,stage.stageHeight * 0.47,5);//x,y,speed
// make level object exist
level1 = new Level1();
// place level1 on screen
addChild(level1);
// place character on screen
addChild(character);
// listen for movements and animations
addEventListener(Event.ENTER_FRAME,gameLoop);
OTimer1 = new Timer(30000);
OTimer1.addEventListener(TimerEvent.TIMER, timeOver);
OTimer1.start();
}
// Handle game related stuff
public function gameLoop(e:Event)
{
//hand and scythe sound play
if (handNow)
{
channel2 = handScream.play(0,0);
handNow = !handNow;
}
if (scytheNow)
{
channel3 = scytheScream.play(0,0);
scytheNow = !scytheNow;
}
if (character && character.cc_mc && level1)
{
if (level1.hand.grabbing_mc && level1.hand.grabbing_mc.hc_mc && character.cc_mc.hitTestObject(level1.hand.grabbing_mc.hc_mc))
{
if (level1.hand.grabbing_mc.currentFrame == 1)
{
handNow = true;
}
trace("caught by hand");
}
else if (level1.scythe.sc_mc && character.cc_mc.hitTestObject(level1.scythe.sc_mc))
{
if (level1.scythe.currentFrame == 1)
{
scytheNow = true;
}
trace("hurt by blade");
}
else if (level1.treeMonkey.swing_mc || level1.treeMonkey.static_mc)
{
if (level1.treeMonkey.swing_mc && level1.treeMonkey.swing_mc.tmc_mc && character.cc_mc.hitTestObject(level1.treeMonkey.swing_mc.tmc_mc))
{
character.gotoAndStop("Swing");
}
else if (level1.treeMonkey.static_mc && level1.treeMonkey.static_mc.missDeath_mc && character.cc_mc.hitTestObject(level1.treeMonkey.static_mc.missDeath_mc))
{
trace("fell in crack");
}
}
}
else if (character && character.cc_mc && level2)
{
//handNow = false;
if (level2.hand.grabbing_mc && level2.hand.grabbing_mc.hc_mc && character.cc_mc.hitTestObject(level2.hand.grabbing_mc.hc_mc))
{
if (level2.hand.grabbing_mc.currentFrame == 1)
{
handNow = true;
}
trace("caught by hand");
}
else if (level2.scythe.sc_mc && character.cc_mc.hitTestObject(level2.scythe.sc_mc))
{
if (level2.scythe.currentFrame == 1)
{
scytheNow = true;
}
trace("hurt by blade");
}
}
// check if the player collides with levelProgression box
if ( character && level1 && level1.levelProgression_mc && character.hitTestObject( level1.levelProgression_mc ))
{
// player collides with levelProgression box
removeChild(level1);// remove level 1
level1 = null;// set to null
//cancel amd reset first timer
OTimer1.removeEventListener(TimerEvent.TIMER, timeOver);
OTimer1.reset();
level2 = new Level2();
loadLevelCollision(level2);
addChild(level2);
//change z order
swapChildren(level2,character);
character.x = stage.stageWidth * 0.04,character.y = stage.stageHeight * 0.1;
//start second timer
OTimer2 = new Timer(25000);
OTimer2.addEventListener(TimerEvent.TIMER, timeOver);
OTimer2.start();
//health text to front
this.setChildIndex(health_txt,this.numChildren - 1);
}
}//closes gameLoop
}//closes class screengame
}//closes package