Skip to main content
DJ Gecko
Inspiring
November 26, 2007
Question

How do I reference a MovieClip from a class

  • November 26, 2007
  • 6 replies
  • 511 views
Hi. Trying to learn AS3 and having a hard time ; (

I'm working on a game. I have a Ship class that uses addChild to add a linked Bullet MovieClip to the stage. The Bullet MC is linked to the class below. I've got it working so it moves up the screen, but now I need to build a hitTest, however I cant get access to a movie clip I've placed on the stage, "enemyDisplay_mc". How do I reference this MC? In AS2 I would just do a _root.

Thanks!



package {
import flash.display.*;
import flash.events.Event;
//
public class Weapons extends MovieClip {
private var _thisWeaponMC:MovieClip;
private var _enemyHit:DisplayObject;
private var _speed:Number;
public function Weapons () {
this.addEventListener (Event.ADDED,Initialize);
}
//
private function Initialize (event:Event):void {
//trace ("Weapons");
_thisWeaponMC = MovieClip(this.parent.getChildByName(this.name));
trace ("_thisWeaponMC: " + _thisWeaponMC);
_thisParent = event.currentTarget.parent;
trace ("_thisParent: " + _thisParent);
_enemyHit = this.parent.getChildByName("enemyDisplay_mc");
trace ("_enemyHit: " + _enemyHit);
_speed=20;
this.addEventListener (Event.ENTER_FRAME,moveShip);
}
//
private function moveShip (event:Event):void {
this.y-= _speed;
}
}
}
This topic has been closed for replies.

6 replies

kglad
Community Expert
Community Expert
November 27, 2007
well, something has to have access to the main timeline. pass a main timeline reference back to Weapons or create a global variable for the main timeline and use that variable whereever it's needed.
kglad
Community Expert
Community Expert
November 27, 2007
where's the code that instantiates a Weapons class instance?
DJ Gecko
DJ GeckoAuthor
Inspiring
November 27, 2007
This code is called from my Ship class.

The Ship class is the Base class of the ship movie clip.

VulcanShot is the linkage class name of the bullet movie, it's base class is Weapons.

Thanks!
kglad
Community Expert
Community Expert
November 26, 2007
show your code using the attach code option.
DJ Gecko
DJ GeckoAuthor
Inspiring
November 26, 2007
a bullet movie clip is linked to this class. And a Ship class, that is linked to a MC on the stage, fires, or addChild, the linked bullet movie to the stage.

Thanks!
DJ Gecko
DJ GeckoAuthor
Inspiring
November 26, 2007
Thanks. I did as you suggested but now I get errors. I get an error from the linked MC - 1203: No default constructor found in base class Weapons.
DJ Gecko
DJ GeckoAuthor
Inspiring
November 26, 2007
How do I do that?

Thanks!
kglad
Community Expert
Community Expert
November 26, 2007
:

kglad
Community Expert
Community Expert
November 26, 2007
you need to pass a reference to the stage when you create a Weapons instance.