View Full Version : [AS2] help with collision test
kendyb
12-10-2008, 02:26 AM
I am using classes in my flash game. I try to use the hitTest function but it does not work for me. Can someone please help me. In the following code, I have an array in the class ship called enemies that and I need collision between the missiles and the enemies passed on to the array. Here is my missile class:
class missile extends MovieClip
{
//speed of missle
var msleSpeed;
//function to set missle speed
function onLoad()
{
msleSpeed = 25;
}
//function to have missile actions
function onEnterFrame()
{
//speed missile moves at
_x += msleSpeed;
//if missile movieClip greater than 600 pixels, remove missile movieClip from stage
if(_x > 1000)
{
//to remove missile movieClip
this.removeMovieClip();
}
//check the enemies array in the ship class, if this hits enemy1, enemy1 explode
for(var i in _root.ship.enemies)
{
if(this.hitTest(_root.ship.enemies[i]))
{
//remove the shot
this.removeMovieClip();
//find the enemy ship in the array declared in the ship class and call the explode function
_root.ship.enemies[i].explode();
_root.ship.updateScore(50);
}
}
}
}
pradvan
12-12-2008, 03:28 PM
Please post your code in code tags.
FluffyPapes
12-23-2008, 10:00 PM
Well, it would help if you yourself could understand the code.
From what I know, this is copied and pasted off of Kongregate's [www.kongregate.com] "Shootorial".
Either way, the shootorial goes completely in depth with what each thing does. Try going back to Shootorial #5, where it first talks about the shooting and the collision detectings.
killermelga
05-20-2010, 11:17 PM
EDIT, NOW WITH ALL THE CODES
i made all they said on the shootorial, and i think i understood every step, but i still cant make my missiles collide with the enemyes, even though my ship collides with them
MISSILE:
class missile extends MovieClip
{
var speed;
function onLoad()
{
speed = 20
}
function onEnterFrame()
{
_x += speed;
for(var i in _root.ship.enemies)
{
if(this.hitTest( _root.ship.enemies[i] ) )
{
this.removeMovieClip();
_root.ship.enemies[i].explode();
}
}
if(_x > 600)
{
this.removeMovieClip();
}
}
}
HERO SHIP:
class ship extends MovieClip
{
var velocity;
var shootLimiter;
var enemyTimer;
var enemies;
function onLoad()
{
velocity = 10;
shootLimiter = 0;
enemyTimer = 0;
enemies = [];
}
function onEnterFrame()
{
if( Key.isDown(Key.RIGHT) )
{
_x = _x + velocity;
}
if( Key.isDown(Key.LEFT) )
{
_x = _x - velocity;
}
if( Key.isDown(Key.UP) )
{
_y = _y - velocity;
}
if( Key.isDown(Key.DOWN) )
{
_y = _y + velocity;
}
shootLimiter += 1;
if( Key.isDown(Key.SPACE) && shootLimiter > 8 )
{
shootLimiter = 0
var missile = _root.attachMovie( "missile" , "missile" + _root.getNextHighestDepth(), _root.getNextHighestDepth() );
missile._x =_x + 50;
missile._y=_y + 2
}
enemyTimer += 1
if(enemyTimer > 60)
{
enemyTimer = 0
var enemy = _root.attachMovie("EnemyShip", "EnemyShip" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
enemies.push(enemy);
}
}
}
ENEMY SHIP:
class EnemyShip extends MovieClip
{
var speed;
function onLoad()
{
_x = 700;
_y = Math.random()*200 + 50
speed = Math.random()*5 + 5
}
function onEnterFrame()
{
_x -= speed;
if(_x < -100)
{
this.removeMovieClip();
}
if(this.hitTest(_root.Ship))
{
explode();
}
}
function explode()
{
var explosion = _root.attachMovie( "Explosion" , "Explosion" + _root.getNextHighestDepth(), _root.getNextHighestDepth() );
explosion._x = _x;
explosion._y = _y;
this.removeMovieClip();
}
}
EXPLOSION:
class Explosion extends MovieClip
{
function onEnterFrame()
{
if(this._currentframe == this._totalframes)
{
this.removeMovieClip();
}
}
}
pradvan
05-21-2010, 02:18 PM
killermelga, where is the rest of the code? can't really tell what's wrong without seeing the rest of it
killermelga
05-22-2010, 07:07 PM
pradvan, now with all the codes
pradvan
05-24-2010, 08:30 PM
Inside your missle class, add this trace:
class missile extends MovieClip
{
var speed;
function onLoad() {
speed=20;
trace(">>> " + _root.ship.enemies);
}
function onEnterFrame()
{
_x+=speed;
for (var i in _root.ship.enemies) {
if (this.hitTest(_root.ship.enemies[i])) {
this.removeMovieClip();
_root.ship.enemies[i].explode();
}
}
if (_x>600) {
this.removeMovieClip();
}
}
}
tell me what you get from that trace
killermelga
05-24-2010, 09:28 PM
i get ">>> undefined" on the output console
what does it mean?
pradvan
05-24-2010, 10:02 PM
it means that your missile class doesn't see "_root.ship.enemies".
So when you do a hitTest, it doesn't work because it can't see the enemies
killermelga
05-24-2010, 10:15 PM
so, can u see what can i do to solve it?
pradvan
05-24-2010, 10:57 PM
Luckily I guessed what was wrong without seeing the fla. Can't really fix it without playing around with the fla
killermelga
06-04-2010, 12:27 AM
how can i upload the file?
rar files arent supported and with zip i cant have one file under 500mb
Huijari
06-12-2010, 10:33 PM
attach it as .fla with attach in "additional options" here on as forums
killermelga
06-13-2010, 03:42 PM
here it is the fla file
Huijari
06-13-2010, 10:11 PM
cant open it, some winrar error, could you post not in zip
killermelga
06-14-2010, 03:31 PM
but on additional options, .fla isnt a recognized file extension, as well as .rar
Huijari
06-14-2010, 07:45 PM
post it on some files uploading site then
killermelga
06-15-2010, 10:09 PM
lol, i never thought of that xp
Download ship.fla gratuito em uploading.com (http://uploading.com/files/6cd2b6ae/ship.fla/)
Huijari
06-15-2010, 10:44 PM
can't open, im using cs4 could you post it as it?
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.