How do I get a top-down-shooter bullet to collide with an enemy? AS3 - actionscript-3

My enemies slowly move down the screen from the top of the stage, and the player is down the bottom, firing bullets. I'm having an issue with getting the bullets to actually collide with the enemy, and remove both objects from the stage?
This is my code so far:
for(var i = 4; i<core.numChildren; i++){
var target:MovieClip = core.getChildAt(i);
if (this.hitTestObject(target)){
//remove listeners
this.removeEventListener(Event.ADDED_TO_STAGE, onStart);
this.removeEventListener(Event.ENTER_FRAME,loop);
//remove bullet
core.removeChild(target);
//remove zombie
core.removeChild(this);
Any help will be greatly appreciated! Thankyou :)

Related

Sounds still playing in another frame

Hey I am still getting some unwanted sound effects still playing in another frame, for example when I click my left mouse button which is also my jump button it will play the jump sound as well as playing the collect coins sound wierdly even though i remove each child from the stage when going to the game over screen.
Now im a bit unfamiliar with the sound channel so if its needed to be used please be kind and explain :)
In Frame 1:
var myMusic1:Sound = new Game_Over_Noise();
var myMusic2:Sound = new Jump_Noise();
var myMusic3:Sound = new Coin_Noise();
var myMusic4:Sound = new Power_Up_Noise();
var myMusic5:Sound = new Theme();
var channel:SoundChannel = myMusic5.play();
In Frame 8, Game Screen:
function doJump(evt:MouseEvent):void
{
if(!isJumping) //If the player is jumping.
{
jumpPower = 30; //Jump power is equal to 30.
isJumping = true; //isJumping variable is also equal to true.
var channel:SoundChannel = myMusic2.play(); //Play sound effect.
}
}
function update(evt:Event):void
{
if(isJumping) //If the player is jumping.
{
MainChar.y -= jumpPower; //Subtract the value of jumpPower from the player's y co-ordinate.
jumpPower -= 2; //Decrease the value of jumppower by 2.
}
if(MainChar.y + gravity < ground) //If the value of the player's Y co-ordinate and gravity is less than ground.
MainChar.y += gravity; //Then add the value of gravity to the player's Y co-ordinates.
else //else
{
MainChar.y = ground; //The players Y co-ordinate is equal to ground.
isJumping = false; //Make isJumping equal to false.
}
}
in Frame 5, Game Over Screen:
SoundMixer.stopAll();
Now this stops the theme music and not the sound effects, now i actually dont mind the theme tune playing all the time but i would like ALL sound effects (game sounds) to only play in the game.
I know my coding isnt the best and efficient but its easily readible to me, I appreciate the help! :D
Looks to me like you need to remove your event listeners (they're active even when something is off-stage).

AS3 / removeChild/addChild by clicking button

I have several movie clips into a frame the size of the stage and I have to switch through a button between those pages.
So if I press button, should all the other frames removeChild and the one where he is called to go addChild.
Edit: I have the actionscript placed in the timeline of the movieClip so the button is not on the stage but I put in the movie clip using action script.
So what DodgerThud showed here is not possible because the button has changed since that is in the movieClip('s).
I think I need to place the same code in every movieClip.
Put all of your MovieClips into a Vector or Array.
When you click the button, you should cycle through the Vector/Array and check if the MovieClip is currently on stage with contains(DisplayObject). If the Movieclip IS currently on the stage, remove it and add another one to the stage, for example, the next one in the Vector/Array.
var vec:Vector.<MovieClip> = new Vector.<MovieClip>
vec[0] = new MovieClip();
vec[1] = new MovieClip(); //example with MovieClips
vec[2] = new MovieClip();
addChild(vec[0]); //add one of the MovieClips to stage
button.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
for(var i:int = 0; i < vec.length; i++) //go through the Vector one by one
{
if(contains(vec[i]) //if the Object at position i in the Vector is on stage
{
removeChild(vec[i]); //remove the Object
var next:int = i; //create a temporary holder
if(next == vec.length) //check if the displayed Object was the last in the list
{
next = 0; //if so, set to 0
}else{
next++; //otherwise, only add 1
}
addChild(vec[next]); //add the next Object to the stage. If the removed Object was the last in the Vector, it'll add the first Object in the Vector to the list
break; //escape the for loop, otherwise it'll always only show the last Object
}
}
}
Something like ...
function tueHandler(e:MouseEvent):void
{
while(numChildren > 0)
removeChildAt(0);
addChild(whatever);
}

How can I work this out?

I am making a game where insects from above the screen move downwards to the bottom. The object is for the player to kill these insects with his/her mouse. When killed the insect should show a kill frame, where I have put in. The kill frame will stay there for 3 seconds and the object will be removed. This will also increase the player's score.
This code is written inside the insect:
function kill(event:MouseEvent):void
{
this.dead = true;
}
This code is written inside the background movieclip frame.
function moveEnemies():void
{
var tempEnemy:MovieClip;
for (var i:int =enemies.length-1; i>=0; i--)
{
tempEnemy = enemies[i];
if (tempEnemy.dead)
{
tempEnemy.gotoAndStop(21);
var myTimer:Timer = new Timer(3000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
}
}
}
function timerListener (e:TimerEvent):void
{
for (var i:int =enemies.length-1; i>=0; i--)
{
if (tempEnemy.dead)
{
score++;
roachLevel.score_txt.text = String(score);
removeEnemy(i);
}
}
}
function removeEnemy(id:int)
{
removeChild(enemies[id]);
enemies.splice(id,1);
}
The problem that I am experiencing with this, is that whenever I click on the insects they stay there. I click on another one it stays there. Then another, then after a while they disappear and the score increases. Sometimes the death frame doesn't appear and they die as soon as I touch them. Could you please tell me how I can solve this?
You have to relocate score adding to enemy class, and assign a timeout to the particular squished enemy instead of the base class. The best place to do it is in kill() function.
function kill(e:MouseEvent):void {
this.dead=true;
gotoAndStop(21);
flash.utils.setTimeout(removeSelf,3000);
}
function removeSelf():void {
this.parent.removeChild(this);
}
Now, adjust the mechanics for scoring and tracking objects. First, if an object becomes dead, you immediately remove it from the array of enemies (it will handle its removal on its own) and give score for it. And second, all the other functional should be preserved.
for (var i:int=enemies.length-1;i>=0;i--) {
var tempEnemy=enemies[i];
if (tempEnemy.dead) {
score++;
enemies.splice(i,1);
} else {
tempEnemy.y++; // or other move function
}
}
This loop should be in an enterframe listener, so it'll be called once per frame. Also combining splicing and moving like here eliminates the need of placing movement code elsewhere, as you iterate through the array anyway.

Movieclips freeze during tween

I've written this thing that displays little star graphics randomly, tweens them down the screen, and then removes them. I'm having a problem though where after maybe 10 seconds some of the stars freeze in place mid tween and then just stay there.
Here's my code:
// Create Random Variables.
var xPosition:int;
var yPosition:int;
// Animate Stars.
function stars ():void {
//Defines random starting position for stars.
xPosition = Math.floor(Math.random()*(540))+5;
yPosition = Math.floor(Math.random()*(2))+5;
//Add and position stars.
var newStar:star = new star();
newStar.x = xPosition;
newStar.y = yPosition;
addChild(newStar);
//Tween stars.
var tweenStar:Tween = new Tween(newStar, "y", None.easeOut, yPosition, stage.stageHeight, 4, true);
//Event listener checks star tween.
tweenStar.addEventListener(TweenEvent.MOTION_FINISH, removeStar);
//Remove stars when tween is complete.
function removeStar(e:TweenEvent):void {
removeChild(newStar);
tweenStar.removeEventListener(TweenEvent.MOTION_FINISH, removeStar);
}
}
Your tweens are being picked up by the garbage collector, what you need to do is create a global array to store your tweens after you create them so the garbage colletor does not get them.
var tweens:Array = [];
and then add tweens to it
var tweenStar:Tween = new Tween(newStar, "y", None.easeOut, yPosition, stage.stageHeight, 4, true);
tweens.push(tweenStar)
Also if possible use TweenLite, it's a lot better then Adobe's standard tween, and you won't have to worry about losing tweens to the garbage collector.

AS3 laser weapon

I am pretty new to action script 3 (I did a little bit in as2) and i am trying to create a laser gun that rotates towards the mouse and shoots when the mouse is fired.
kind of like this but in as3
http://www.freeactionscript.com/2009/04/laser-hitting-solid-objects-walls/
Thanks,
Thor
You can start to play with something like this:
//adding listener to run all the time
m.addEventListener("enterFrame", runGame);
//listener for mouse is down
stage.addEventListener("mouseDown", md);
//listener for mouse is up
stage.addEventListener("mouseUp", mu);
//to know if mouse is down
var _fire:Boolean = false;
//function for all the time
function runGame(evt:*):void
{
//to know the angle where the mouse is from the "canon" in radians
var angle:Number = Math.atan2(stage.mouseY - m.y, stage.mouseX - m.x);
//set the canon's rotation
m.rotation = 180 * angle / Math.PI;
//if mouse us down -> fire
if(_fire)
{
//create a point far away, so it will always work if the screen is normal
var point:Point = Point.polar(10000, angle);
//shoot the laser
graphics.lineTo(point.x, point.y);
}
}
//when mouse is down
function md(evt:*):void
{
//prepare graphics
graphics.clear();
graphics.lineStyle(2, 0xff0000);
graphics.moveTo(m.x, m.y);
//set fire to true
_fire = true;
}
//when mouse is up
function mu(evt:*):void
{
//set fire to false
_fire = false;
//clear the laser
graphics.clear();
}
Create a new project, place a movieclip on the stage, name it "m" (without the quotes ;)). Paste the script where the actionscript goes on frame 1.
If you want the laser to follow the mouse when it's down, change
runGame function to this:
function runGame(evt:*):void
{
//to know the angle where the mouse is from the "canon" in radians
var angle:Number = Math.atan2(stage.mouseY - m.y, stage.mouseX - m.x);
//set the canon's rotation
m.rotation = 180 * angle / Math.PI;
//if mouse us down -> fire
if(_fire)
{
//create a point far away, so it will always work if the screen is normal
var point:Point = Point.polar(10000, angle);
//shoot the laser
graphics.clear();
graphics.lineStyle(2, 0xff0000);
graphics.moveTo(m.x, m.y);
graphics.lineTo(point.x, point.y);
}
}
and md function to this:
//when mouse is down
function md(evt:*):void
{
//set fire to true
_fire = true;
}
"Epic" laser, with source. Also great site btw.
Probably best to learn the basic concepts of programming before trying to tackle things like this. Cutting/Pasting code is not programming, and can have frankenstein-ish results.
There is no magical shortcut to programming, you need to learn the basic concepts and build on them. Wonderfl.net is a cool place to check out, but I'd suggest starting out far more basic concepts to start. Until you do that, it's all going to appear as voodoo to you.