I’d been working on a side-scrolling shooter game in AS2 for a while and it was going great, but for whatever reason, it scrolls all choppy and the framerate is inconsistent on faster machines. I would have expected the opposite, but it is what is is, I guess. So I’m trying to finally learn AS3, and I found some tutorials that got me started towards remaking what I had, but now I’m stuck.
I’m trying to make one of those rotating cannons that shoots projectiles towards the mouse cursor. I have the rotation part working, but I’m having trouble with attaching the projectiles to the stage with enterFrame actions on them.
I feel like I’m pretty close to a solution, but I don’t understand where to put the functions to move the clips to make them work properly.
In AS2, I just put something like this on the cannon clip:
bullet = _root.attachMovie("bullet", "bullet"+_root.bullets, _root.getNextHighestDepth(), {_x:nozzledot.pointx, _y:nozzledot.pointy});
bullet.moveVector = this.shootvector;
_root.bullets++
bullet.onEnterFrame = function() {
this.Bullet(); //Bullet() is a prototype function that does the vector math to move the bullet in the appropriate direction.
};
And that worked fine.
But now I have these going on…
A bullet class…
package
{
import flash.display.MovieClip;
import flash.events.*;
public class Bullet extends MovieClip
{
public var _moveVector:Object;
public var _moveSpeed:Number;
public var startX:Number;
public var startY:Number;
public function Bullet():void
{
}
}
}
which I’m thinking will work the way I intend, so that every instance has those variables set up, which are assigned here, in this mouse event handler in the main .as file…
private function mouseClickHandler(e:Event):void
{
var _bullet = new Bullet();
_bullet._moveVector = angleToVector(_cannon.rotation);
_bullet._moveSpeed = 3;
_bullet.startX = _cannon.x;
_bullet.startY = _cannon.y;
// I'll have another clip at the cannon's tip with some getbounds action to make these come
// out at the right places, but for now, I just want to get them working again.
_bullet.name = "_bullet"+_bulletsactive;
_bullet.x = _bullet.startX;
_bullet.y = _bullet.startY;
_bulletsactive++
stage.addChild(_bullet);
}
Those parts seem to work fine (as in, it runs without error messages). But the problem is with the function to make them actually do stuff.
When I add an ENTER_FRAME event listener in the Bullet class, it gives
me this: “TypeError: Error #1009: Cannot access a property or method of a
null object reference. at Bullet/enterFrameHandler()”.
If I put it in documentMain.as, as part of the mouse event stuff,
there’s nowhere to put the actual function after adding the listener,
because I can’t define the function within the mouse event handler
function, and outside of that function, I lose the _bullet variable to
contextualize the stuff in the function, so when I go:
...
_bullet.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
stage.addChild(_bullet);
}
public function bulletEnterFrame(e:Event):void
{
x += _moveVector.vx * _moveSpeed;
y += _moveVector.vy * _moveSpeed;
}
I get: “1120: Access of undefined property _moveVector.” (and
_moveSpeed), I’m guessing because now those don’t refer to anything. (I
thought they’d be taken like “this.” used to be, and then when something
tried to use them that DID have those properties, it would work, since I’m not trying to run them until an instance exists-- but evidently not.)
This is probably a really stupid question in the eyes of people who
are used to this kind of stuff, but I’m completely lost. How can I make
this work?
EDIT:
Okay, nevermind. I think I figured it out. I added the frame event listener within an ADDED_TO_STAGE handler, and removed an instance of the associated movie clip from the stage that was causing the null reference error. That fixed it. It doesn’t work as intended, but it’s doing the stuff from the enterframe events now. My bad. : )