In my Flash program, I am trying to run a function within an object. The object uses a class called “SkullDemon.as.” The function I am trying to run is called “moveSkullDemon();” this function is coded within the “SkullDemon.as” class.
My main document class is “TME2d_Main.as.” Within that file I create an instance of the Skull Demon class like this:
public var skullD1b:SkullDemon;
skullD1b = new SkullDemon();
skullD1b.x = 2990.75;
skullD1b.y = 836.95;
skullDemonContainer.addChild(skullD1b);
The ‘skullDemonContainer’, which stores the SkullDemon instances, is placed into another container, called ‘envContainer,’ which is added to the screen using this line of code:
this.addChild(envContainer);
The SkullDemon instance is created and loads onto the screen just fine. The problem occurs when I try to run any function within the SkullDemon class.
As mentioned before, I try to call the ‘SkullDemon.moveSkullDemon()’ function for the ‘skullD1b’ instance. For now, the ‘moveSkullDemon()’ function just moves the SkullDemon object to the left.
I tried calling this function from the SkullDemon’s constructor function, but that does not work. In fact, any code written within the SkullDemon’s constructor is not executed when a Skull Demon object is created. I have tried to create an EventListener within the SkullDemon class that calls ‘moveSkullDemon(),’ but that does nothing.
I have tried the following methods to get the ‘moveSkullDemon()’ function to run:
1: Calling ‘moveSkullDemon()’ from ‘TME2d_Main’ like this:
skullD1b.moveSkullDemon();
But this brings up an error (#1006), saying that ‘moveSkullDemon’ is not a function.
2: Doing the same thing as #1 except that I have no parenthesis within the function call:
skullD1b.moveSkullDemon;
3: Creating an EventListener within ‘TME2d_Main’ for the ‘skullD1b’ instance that calls its ‘moveSkullDemon’ function:
skullD1b.addEventListener(Event.ADDED, skullD1b.moveSkullDemon);
This brings up another error: “Error #2007: Parameter listener must be non-null.” It also constantly finds a “null” reference error:
"1009: Cannot access a property or method of a null object reference.
at classes::TME2d_Main/controlPlayer()"
I do not know why this problem refers to the ‘controlPlayer()’ method, as it does not involve the Skull Demon instance at all. ‘controlPlayer()’ is called through an EventListener with this code within ‘TME2d_Main’:
player.addEventListener(Event.ENTER_FRAME, controlPlayer);
4: Creating an EventListener for the ‘SkullDemonContainer’ that calls the move function:
skullDemonContainer.addEventListener(Event.ADDED, skullD1b.moveSkullDemon);
This brings up the same error as in #3.
I have been having this problem for a few days now and do not know what is causing it. Here is my code within the ‘moveSkullDemon()’ function:
public function moveSkullDemon() {
trace("skull demon moving!");
this.x -= 5;
// If the Player has not hit the 'floor,' increase his falling
//speed
if (! floor.hitTestPoint(this.x, this.y, true)) {
this.y += this.sdGravity;
// The Skull Demon is not on the ground when he's not touching it
sdOnGround = false;
}
// Increase the 'sdYVel' variable so that the Skull Demon will fall
// progressively faster down the screen. This code technically
// runs "all the time" but in reality it only affects the Skull Demon
// when he's off the ground.
sdYVel += sdGravity;
// Increase the Skull Demon's 'y' coordinate by the 'sdYVel' value
if (! sdOnGround) {
this.y += sdYVel;
}
}
Thanks for any help you may offer.
If I understand right, you are attempting to dispatch an event against your display object, therefore calling a function?
When you instantiate your skull demon object, add an event listener for the type of event you wish to handle. For example, if you created a “DemonEvent” class:
When you want to call the “moveDemonHandler” function, you dispatch an event against the object:
You can place code within the event handler, or call your function from the event handler code:
Example “DemonEvent” if you need:
UPDATE
Based upon your comment, it sounds like you are determining the best place to put code to move your object. Your skull demon could move itself, or since your TME2d_Main class adds the skull demon, positions it initially, and because you need to hit test against a floor, it might make more sense to put that logic in the TME2d_Main class.
This example doesn’t use events; however, hopefully helps you get started. Let me know if you’d rather have an example that uses events instead.
here are two classes as an example implementation:
TME2d_Main.as
SkullDemon.as