hi guys,
I’m a beginner in AS3 only been using as3 for 2 days and I have a problem regarding hitTest
I’m trying to make a First person avoider game where you try to dodge incoming meteors.
Everything is fine, I was able to make the meteors hit the ship I was controlling and I was also able to make the meteors explode upon impact.
The problem is when I try to make the ship “juggle” everytime a meteor hits.
heres the AS file of meteor
package com.basics1
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Meteors extends MovieClip
{
private var stageRef:Stage;
private var target:Ship;
var movement:int;
var checking:int = 0;
var divider:Number = 0;
public function Meteors(stageRef:Stage, target:Ship) : void
{
this.target = target;
this.alpha = 0;
x = Math.floor(Math.random() * (280 - 180 + 1)) + 180;
y = Math.floor(Math.random() * (250 - 152 + 1)) + 152;
movement = Math.floor(Math.random() * (8 - 1 + 1)) + 1;
divider = Math.floor(Math.random() * (7 - 3 + 1)) + 3;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
this.alpha += .05;
if(scaleX < 50)
{
scaleX += .5;
scaleY += .5;
}
if(movement == 1)
{
//move right
x += scaleX/divider;
}
else if(movement == 2)
{
//move upright
x += scaleX/divider;
y += scaleX/divider;
}
else if(movement == 3)
{
//move up
y += scaleX/divider;
}
else if(movement == 4)
{
//move left up
x -= scaleX/divider;
y += scaleX/divider;
}
else if(movement == 5)
{
//move left
x -= scaleX/divider;
}
else if(movement == 6)
{
//move left down
x -= scaleX/divider;
y -= scaleX/divider;
}
else if(movement == 7)
{
//move down
y -= scaleX/divider;
}
else if(movement == 8)
{
//move right down
x += scaleX/divider;
y -= scaleX/divider;
}
else
{
x -= scaleX/divider;
}
if(checking == 0)
{
rotation += 4;
}
else
{
rotation = 0;
}
if(scaleX >= 20)
{
if (this.hitTestObject(target.hitter) && checking == 0)
{
if(checking == 0)
{
checking = 1;
this.gotoAndStop(2);// makes this meteor explode
target.gotoAndStop(2); // makes ship juggle when hit.
}
}
}
if(x > 650 || x < -100)
{
removeSelf();
}
if(y > 460 || y < -80)
{
removeSelf();
}
}
private function removeSelf() : void
{
this.removeEventListener('enterFrame', loop)
if (parent != null)
{
parent.removeChild(this);
}
}
}
}
The problem is that “target.gotoAndStop(2);” executes even if the ship is not being hit.
Another weird thing is I tried to trace the value of checking and it turns to 1 even if the condition is false. :hat:
This is how I create the meteor
package com.basics1
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import com.particles.Starfield;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.geom.ColorTransform;
public class Engine extends MovieClip
{
var cnt:int = 0;
var ourShip:Ship = new Ship(stage);
var randnum:int;
var randnum2:int;
public function Engine()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
stage.addChild(ourShip);
ourShip.x = stage.stageWidth/2;
ourShip.y = stage.stageHeight/2;
addChild(new Starfield());
}
public function loop(e:Event) : void
{
var newMeteor:Meteors = new Meteors(stage, ourShip);
randnum = Math.floor(Math.random() * (20 - 1 + 1)) + 1;
if(randnum == 5 && cnt < 10)
{
trace(randnum);
cnt += 1;
stage.addChild(newMeteor);
newMeteor.name = "meteor" + cnt;
stage.setChildIndex(newMeteor,1);
if(cnt == 10)
{
cnt = 0;
}
}
}
}
}