Author Topic: Problem with Flash(AS2.0)  (Read 1239 times)

0 Members and 1 Guest are viewing this topic.

DarkCrusade

  • Guest
Problem with Flash(AS2.0)
« on: October 07, 2010, 08:01:47 am »
Hey guys, I hope you can help me a bit. This is my first attempt at making a game with Flash (ActionScript 2.0), but I encountered a bug that I cannot seem to fix myself. The error must lie in the for..in loop in the missile's class.. (you can see the game in action here).

Main class that controls movement of the player's ship, shooting bullets and spawning enemies. The array enemies
contains all enemy-ships on the screen. Unfortunatly, the size of the array rises every time an enemy spawns, but
doesn't get smaller when one is removed from screen..
Code: [Select]
class Ship extends MovieClip
{
  var velocity = 10;
  var shoottimer = 0;
  var enemytimer = 0;
  var enemies = [];
 
  function Explode(){
    var explosion = _root.attachMovie("Explosion","Explosion"+_root.getNextHighestDepth(),_root.getNextHighestDepth());
    explosion._x = _x;
    explosion._y = _y;
    this.removeMovieClip();  
  }
 
  function onEnterFrame(){
    shoottimer += 1;
    enemytimer += 1;
    if (Key.isDown(Key.RIGHT)){ _x += velocity; }
    if (Key.isDown(Key.LEFT)){ _x -= velocity; }
    if (Key.isDown(Key.UP)){ _y -= velocity; }
    if (Key.isDown(Key.DOWN)){ _y += velocity; }
    if (Key.isDown(Key.SPACE) && (shoottimer > 8)){
      shoottimer = 0;
      var missile = _root.attachMovie("Missile","Missile"+_root.getNextHighestDepth(),_root.getNextHighestDepth());
      missile._x = _x + 101;
      missile._y = _y + 37;
    }
    if (enemytimer >= 65){
      enemytimer = 0;
      var enemy = _root.attachMovie("EnemyShip","EnemyShip"+_root.getNextHighestDepth(),_root.getNextHighestDepth());
      enemies.push(enemy);
    }
  }
}

This class extends the enemy-ships. It controls their movement and checks whether the ship is in contact with
the player's ship. No problem here so far..
Code: [Select]
class EnemyShip extends MovieClip{
  var speed;
 
  function Explode(){
    var explosion = _root.attachMovie("Explosion","Explosion"+_root.getNextHighestDepth(),_root.getNextHighestDepth());
    explosion._x = _x;
    explosion._y = _y;
    this.removeMovieClip();  
  }
 
  function onLoad(){
    _x = 1050;
    _y = Math.random() * 200 + 50;
    speed = Math.random() * 5 + 2;
  }
 
  function onEnterFrame(){
    _x -= speed;
    if (this.hitTest(_root.mainship)) { Explode(); }
  }
}

This is the bugged class that controls the bullets of the player's ship. The for..in loop is not called once..
Code: [Select]
class Missile extends MovieClip
{
  var speed = 8;
 
  function onEnterFrame()
  { 
    _x += speed;
    if (_x > 1000) { this.removeMovieClip(); }

    for(var i in _root.Ship.enemies){
      if(this.hitTest(_root.Ship.enemies[i])){
        this.removeMovieClip();
        _root.Ship.enemies[i].Explode();
      }
    }
  }
}

Explosion class.. unimportant, but I'll just show you all classes:
Code: [Select]
class Explosion extends MovieClip{
  function onEnterFrame(){ if(this._currentframe == this._totalframes){ this.removeMovieClip();} }
}

Loops the background:
Code: [Select]
class Background extends MovieClip {
  function onEnterFrame(){
    _x -= 1;
    if (_x < -1399){ _x = 0; }
  }
}

Thanks for any help!

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: Problem with Flash(AS2.0)
« Reply #1 on: October 07, 2010, 08:06:50 am »
About first class - I think it's good. This way you can count how many enemies you have killed.

I'll see later what's wrong with bugged class - I'm not sure if I'll be able to help... :)

« Last Edit: October 07, 2010, 08:11:20 am by Wookash »
"My senses are so powerful that I can hear the blood pumping through your veins."

DarkCrusade

  • Guest
Re: Problem with Flash(AS2.0)
« Reply #2 on: October 07, 2010, 08:14:17 am »
My only concern with the first class is, that the size of the array is getting bigger and bigger. I wonder whether it'll cause lags with the hit detection of the missiles later (assuming it'll work somewhen)..

DarkCrusade

  • Guest
Re: Problem with Flash(AS2.0)
« Reply #3 on: October 11, 2010, 08:37:19 am »
Sorry, but I still have the same problem.. but I changed the for..in loop in the Missile-class to a normal for loop using this syntax:

Code: [Select]
for(var i=0; i<_root.Ship.enemies.length; i++) {...}

In addition, I used trace() and finally found the error's cause: _root.Ship.enemies is undefined. In a very strong contrast, trace() in the main class outputs the correct length.
« Last Edit: October 12, 2010, 04:20:47 am by DarkCrusade »

Offline KYnetiK

  • Camper
  • ***
  • Posts: 314
  • Something Else
    • KYnetiK ART
Re: Problem with Flash(AS2.0)
« Reply #4 on: October 12, 2010, 05:18:59 am »
TBH i didnt think you had created an array properly in the first place. I had a suspicion you hadnt defined it as an array, but kept trying to call it like it was.

I usually use something like:
Code: [Select]
var myArray:Array = Array();

Didnt realise it could be as simple as:
Code: [Select]
var myArray= [];

Either I just learnt a great shorthand for working with arrays, or you messed up there ^_^


I asked God for a bike, but I know God doesn’t work that way. So I stole a bike and then asked for forgiveness.
_____________
WARNING! This user may use sarcasm and cynicism in a way you are not accustomed to.

[Barret]  /  [saw]
*Shakes Fist*

DarkCrusade

  • Guest
Re: Problem with Flash(AS2.0)
« Reply #5 on: October 12, 2010, 05:44:50 am »
You can initialize an empty array like this in Flash, according to html-world.de (German site):

Code: [Select]
var myArray = [];
var myArray = new Array();

Offline KYnetiK

  • Camper
  • ***
  • Posts: 314
  • Something Else
    • KYnetiK ART
Re: Problem with Flash(AS2.0)
« Reply #6 on: October 12, 2010, 05:52:42 am »
I learn somethin every day if im not careful :P

I never knew i could just use
Code: [Select]
array = []
I asked God for a bike, but I know God doesn’t work that way. So I stole a bike and then asked for forgiveness.
_____________
WARNING! This user may use sarcasm and cynicism in a way you are not accustomed to.

[Barret]  /  [saw]
*Shakes Fist*