I have the beginning of a game that looks a bit like Asteroids. The first version of the game worked, but was very code-heavy (every object was individually coded) so I decided to move the bulk of the code to document classes.
I wanted to have certain objects interact with eachother (bullets with asteroids, asteroids with ship, etc.) but they can’t find eachother.
Also, as you may notice in like a second from now, the whole code is handled by a single document. I guess I should do something about that as well?
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent
public class Start extends MovieClip
{
public var Ship_MC;
public var Asteroid_MC;
public var Bullet_MC;
public var Shield; [COLOR=Gray]//Ship's hitpoints[/COLOR]
public var HP; [COLOR=Gray]//Asteroid's hitpoints[/COLOR]
public var Dead;
public function Start(){
CreateRing();
CreateShip();
CreateAsteroids();
Main();
}
public function CreateShip():void{
Shield = 200
Dead = false
Ship_MC = new Ship1();
this.addChild(Ship_MC);
[COLOR=Gray] *Snipped. Code creates a ship, basically*[/COLOR]
Ship_MC.addEventListener(Event.ENTER_FRAME,Hurting);
function Hurting (event:Event):void{
[COLOR=Red] if (Asteroid_MC.hitTestPoint(Ship_MC.x,Ship_MC.y,true)) {[/COLOR]
HP -= 50
Shield -= 50
}
}
function CreateAsteroids():void{
for (var i:int=0; i < 120; i++){
Asteroid_MC = new Asteroid();
this.addChild(Asteroid_MC);
HP = (Asteroid_MC.scaleX + Asteroid_MC.scaleY)*20 +100
[COLOR=Gray]*creates asteroids*[/COLOR]
}
}
function Main(){
stage.addEventListener(MouseEvent.CLICK,Shoot);
}
function Shoot(e:MouseEvent):void {
if (Ship_MC.visible == true){
[COLOR=Gray]*creates a bullet*[/COLOR]
Bullet_MC.addEventListener(Event.ENTER_FRAME, Shooting);
function Shooting (e:Event):void {
[COLOR=Gray] *here I have to create a new var for Bullet_MC for some reason.
[/COLOR] [COLOR=Gray]Code still works, though.*[/COLOR]
var bulletMC:MovieClip = MovieClip(e.target);
}
Bullet_MC.addEventListener(Event.ENTER_FRAME, Hitting);
}
}
}
function Hitting (e:Event):void{
[COLOR=Red] if (Bullet_MC.hitTestPoint(Asteroid_MC.x,Asteroid_MC.y,true)) {[/COLOR]
[COLOR=Gray]... but it never is :([/COLOR]
}
}
}
}