Adobe Animate ActionScript 3 Error 1009 for a MovieClip - actionscript-3

I am writing code to have enemies detect collision with the player. In my Enemy class I have the following:
import flash.display.MovieClip;
import flash.events.Event;
public class Enemy extends MovieClip {
var Player: MovieClip;
public function Enemy() {
this.addEventListener(Event.ENTER_FRAME, EnemyUpdate);
}
function setPlayer(_Player: MovieClip) {
Player = _Player;
}
function EnemyUpdate(_event: Event) {
var enemyHit: Boolean = this.hitTestObject(Player.Character.Legs);
if (enemyHit) {
trace("OUCH!!");
}
}
}
In my Main Class, I attempt to send the Player MovieClip to the Enemy Class script using the following:
public function Main() {
enemy.setPlayer(player);
}
The MovieClip enemy has the Enemy script attached to it. When I run the program, the Player variable is null. How do I get the Player to recognize the Player MovieClip?

This happens because you set your player after an Enemy had been instantiated. In Enemy constructor you have EnterFrame listener. To fix the error change your code as follows:
public function Enemy() {
// empty constructor, you can remove it if there is no other logic in it
}
function setPlayer(_Player: MovieClip) {
Player = _Player;
// the Player variable is not null anymore.
this.addEventListener(Event.ENTER_FRAME, EnemyUpdate);
}

Related

Communication between the Document Class and MovieClip Class: error 1009

I am learning how to make MovieClip classes. All I am trying to do is give the MovieClip the ability to move with the left and right arrow keys, but it's not working.
Here is my code for my Document Class Main:
package {
import flash.display.MovieClip;
public class Main extends MovieClip{
var blueBall:Ball = new Ball; //This is line 7
public function Main()
{
addChild(blueBall)
}
}
}
My code for my Ball class is:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Ball extends MovieClip
{
public function Ball()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); //This is Line 11
}
function keyPressed(evt:KeyboardEvent):void
{
switch (evt.keyCode)
{
case Keyboard.RIGHT :
this.x += 10;
break;
case Keyboard.LEFT :
this.x -= 10;
break;
}
}
}
}
When I run this in the debug, I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Ball()[C:\Users\Joel\Desktop\Flash\Projects\DropClassTest\Ball.as:11]
at Main()[C:\Users\Joel\Desktop\Flash\Projects\DropClassTest\Main.as:7]
Now, if I just leave my Main document class blank, and just drag and drop the blueBall Movieclip out of the Library, it works the way I want it to, but once I try to use the document class to do it, it's not working.
Needless to say, I am quite lost, even thought the debugger is telling me what lines are wrong.
Any ideas?
The problem is that you are trying to access the stage from inside the Ball class before the ball is added to the stage. Before an object is added to the stage, it does not have direct access to the stage.
Option 1: You could pass the stage from the main class to the Ball constructor.
Add this to your Ball class' imports:
import flash.display.Stage;
Add a stage parameter to the Ball class' constructor:
public function Ball(stage:Stage):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}
And pass the stage to your Ball instance (in Main)
var blueBall:Ball = new Ball(stage);
Option 2: You can listen for the ADDED_TO_STAGE event inside of the Ball class and add the KEY_DOWN listener once ADDED_TO_STAGE occurs:
Inside the Ball class import the Event class:
import flash.events.Event;
Replace your constructor with this:
public function Ball():void {
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
Then add your KEY_DOWN listener inside the ADDED_TO_STAGE listener:
function addedToStage(evt:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}

calling movieclip of a class from another class, error#1009

i got error: method of null object reference. I'm so confused and don't know what is the real cause.
So i got a player movieClip on the stage which has an instance of "player_mc", which will be pass thru my Document class and into the Player class.
Player.as
import flash.display.*;
import flash.events.*;
public class Player extends MovieClip
{
public var myPlayer:MovieClip;
public function Player(player:MovieClip)
{
myPlayer = player;
addEventListener(Event.ENTER_FRAME,on_enter);
}
Document.as
import flash.display.*;
import Components.Player.Player;
public class Game_Main extends MovieClip
{
public var player:Player;
public function Game_Main()
{
player = new Player(player_mc);
}
}
now here i think is where the problem comes from. I have a green_enemy movieclip on the stage which has base class Enemy.
Enemy.as
import flash.display.MovieClip;
import Components.Player.Player;
import flash.events.Event;
public class Enemy extends MovieClip
{
var theplayer:Player;
public function Enemy()
{
this.addEventListener(Event.ENTER_FRAME,on_enter);
}
public function on_enter(e:Event):void
{
if (this.hitTestObject(theplayer.myPlayer)) //calls player_mc from Player class
{
trace("hi");
}
}
}
what I like to do on Enemy function is when Enemy collides with "player_mc" (which is on the stage) it will do something. Maybe my code is wrong.
Any help/tips will be appreciated. Thanks!
In your Enemy.as I see
var theplayer:Player;
not initialized. It is private, so you can not define it from outside. That means that exception comes from here
this.hitTestObject(theplayer.myPlayer)
You are trying to call myPlayer from null.
Try to define this variable while constructing Enemy class.
To prevent null exception you can check if theplayer is null
public function on_enter(e:Event):void
{
if (theplayer && this.hitTestObject(theplayer.myPlayer))
{
trace("hi");
}
}
To expand on what ZuyEL said, your variable isn't globally defined in your enemy class since you're just adding a var inside your enemy function. Instead, do the following
public var thePlayer:Player;
write the above after your Enemy class, before the Enemy function. Now it is available to the whole class and will no longer be null when you call it later.

Disabling Nested MovieClips After Removing Parent Movieclip

In some of the level MovieClips I have for my Flash game, there is a certain MovieClip that controls a custom-built camera that I've created. Both the camera and the MovieClip function correctly and smoothly. However, whenever a level is completed and removed from the game, I get an Error #1009 not recognizing the checkCameraZoom function. Also, this MovieClip is not added dynamically with code, but rather placed in the specified level MovieClips from the Library before run-time. Is there any possible way to fix this error?
ZoomOutArea Class:
package com.engine.assetHolders
{
import com.engine.documentClass.*;
import flash.display.*;
import flash.events.*;
public class ZoomOutArea extends MovieClip
{
public function ZoomOutArea():void
{
this.visible = false;
this.addEventListener(Event.ADDED_TO_STAGE, initZoomOutArea);
// constructor code
}
public function initZoomOutArea(event:Event):void
{
this.addEventListener(Event.ENTER_FRAME, checkCameraZoom);
}
public function checkCameraZoom(event:Event):void
{
if (Document.getInstance != null)
{
if (this.hitTestObject(MovieClip(parent.parent).player.playerHitArea))
{
this.hitTestZoom(0.6);
}
if (! this.hitTestObject(MovieClip(parent.parent).player.playerHitArea))
{
this.hitTestZoom(1);
}
}
}
public function hitTestZoom(zoomLevel):Number
{
MovieClip(parent.parent).cameraScale = zoomLevel;
return zoomLevel;
}
}
}
You register the class for ENTER_FRAME events when it's added to the stage, but you never unregister it. So that's why it keeps going even after it has been removed from the stage, and has no parent anymore.
You could add another listener for Event.REMOVED_FROM_STAGE and then remove the checkCameraZoom listener:
public function initZoomOutArea(event:Event):void
{
this.addEventListener(Event.ENTER_FRAME, checkCameraZoom);
this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
}
private function onRemoved(event:Event):void
{
this.removeEventListener(Event.ENTER_FRAME, checkCameraZoom);
}

as3 error 1063 with timer

getting error 1061: Call to a possibly undefined method stop through a reference with static type flash.events:TimerEvent.
on my as3 class. I'm just starting to learn as3 and cant figure out whats causing the error. code:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class game extends MovieClip
{
//assign types to var names
//allows values and variables to be acessed in methods
public var as1:astroid;//astroids
public var ship1:ship;//ship
public var timer:Timer;
public function game()
{
//astroid
as1=new astroid();
addChild(as1);
//ship
ship1=new ship();
addChild(ship1);
//timer
timer=new Timer(25);//every n frames
timer.addEventListener( TimerEvent.TIMER, onTick );//attach function to timer
timer.start();//start timer
}
public function onTick( timer:TimerEvent ):void
{
//animate astroid
as1.moveDown();
//move ship
ship1.x = mouseX;
ship1.y = mouseY;
if(ship1.hitTestObject(as1))
{
timer.stop();//error on this line!
}
}
}
}
Rename timer to event in your event handler:
public function onTick( event:TimerEvent ):void
Also, in Flash CS5, go to File > Publish Setting > Flash, and turn on "Permit debugging". That should give you more useful error messages.

Actionscript 3 mouse_over play movie

I'm trying to play a movie clip when I mouse_over it. I can do it fine by doing:
mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);
function mover(e:MouseEvent):void {
mc1.play();
}
But, I want to use the same function for other movie clips, for example, to play movieclip2, movieclip3 etc.
How would I achieve this?
mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc2.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc3.addEventListener(MouseEvent.MOUSE_OVER,mover);
function mover(e:MouseEvent):void {
e.currentTarget.play();
}
You can make a class to encapsulate your logic for example, to access the MovieClip from the calling function, use the property of the Event object
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class PlayMovieClip {
// register the mouse over event with whatever MovieClip you want
public static function register(mc:MovieClip):void{
mc.addEventListener(MouseEvent.MOUSE_OVER,mover);
}
// unregister the event when you dont need it anymore
public static function unregister(mc:MovieClip):void{
mc.removeEventListener(MouseEvent.MOUSE_OVER, mover);
}
// the MouseEvent will be throw whenever the mouse pass over the registered MovieClip
// and in the MouseEvent property you have the targeted object
// so use it
public static function mover(e:MouseEvent):void{
// check if we have really a MovieClip
var mc:MovieClip=e.currentTarget as MovieClip;
if (mc!==null) {
// we have a MovieClip so we can run the function play on it
mc.play();
}
}
}
usage:
PlayMovieClip.register(mc1);
...
PlayMovieClip.register(mcX);
and to remove the event:
PlayMovieClip.unregister(mc1);
...
PlayMovieClip.unregister(mcX);