Issue with Flash AS3 Player movieclip and creating an instance - actionscript-3

So heres my issue, I created a movieclip class within Adobe Animate (Warmage.as) and I added it to my stage (addChild(char)). I tried to access the properties says undefined property of char. But I created a class for Warmage and created an instance of it (char).
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;
public class Main_class extends MovieClip
{
//player stats
var hsp:Number = 0;
var vsp:Number = 0;
var floor:Number = 1318;
var attackCounter = 5;
var doubleJumpCount = 0;
//Player states
var rightSide:Boolean = false;
var rDown:Boolean = false;
var lDown:Boolean = false;
var jumped:Boolean = false;
var onGround:Boolean = false;
var crouchMode:Boolean = false;
var attackMode:Boolean = false;
var canDoubleJump = false;
public function Main_class()
{
var char:Warmage = new Warmage();//Adds player to the level
char.x = 500;
char.y = 300;
addChild(char);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);//Stage listens no matter what
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
function gameLoop(e:Event):void
{
if(rDown)
{
char.x += 10;
}
if(lDown)
{
char.x -= 10;
}
}
function keyPressed(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT)
{
rDown = true;
}
if(e.keyCode == Keyboard.LEFT)
{
lDown = true;
}
if(e.keyCode == Keyboard.UP && onGround)
{
jumped = true;
//doubleJumpCount += 1;
}
if(e.keyCode == Keyboard.DOWN && onGround)
{
crouchMode = true;
}
if(e.keyCode == Keyboard.SPACE && onGround)
{
attackMode = false;
}
}
function keyReleased(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT)
{
rDown = false;
}
if(e.keyCode == Keyboard.LEFT)
{
lDown = false;
}
if(e.keyCode == Keyboard.UP)
{
jumped = true;
}
if(e.keyCode == Keyboard.DOWN)
{
crouchMode = false;
}
if(e.keyCode == Keyboard.SPACE)
{
attackMode = true;
}
}
}
}

Because you're using a local variable.
public function Main_class()
{
var char:Warmage = new Warmage();//Adds player to the level
trace(char); // OK. You can available char inside of this function.
}
function gameLoop(e:Event):void
{
trace(char); // You can not available that variable here.
}
Use global variable. Declare a variable outside a function.
private var char:Warmage;
public function Main_class()
{
char = new Warmage();//Adds player to the level
char.x = 500;
char.y = 300;
addChild(char);
trace(char); // OK
}
function gameLoop(e:Event):void
{
trace(char); // OK
}
Please read "Understanding variable scope"
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html

Related

How to add score when collecting things (Actionscript 3 Adobe Animate)?

How to create an incremented score after he pickup the rubbish? I am very new on this thing and need help on coding.
My code below is on a separated .as file (movieclip).
As you can see, there are codes for when the player hittestobject, the "rubbish" will go away but how to apply a new code for score to that specific function? so the score will be incremented each time he pick up the rubbish.
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class mazeClass extends MovieClip {
var mouseUp:Boolean = false;
var mouseDown:Boolean = false;
var mouseLeft:Boolean = false;
var mouseRight:Boolean = false;
var speed:Number = 5;
public function mazeClass() {
// constructor codea
stage.addEventListener(KeyboardEvent.KEY_DOWN, CheckDownKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, CheckUpKeys);
stage.addEventListener(Event.ENTER_FRAME, updatePos);
}
private function CheckDownKeys(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
mouseUp =true;
}
if(e.keyCode == Keyboard.DOWN){
mouseDown =true;
}
if(e.keyCode == Keyboard.LEFT){
mouseLeft =true;
}
if(e.keyCode == Keyboard.RIGHT){
mouseRight =true;
}
}
private function CheckUpKeys(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
mouseUp =false;
}
if(e.keyCode == Keyboard.DOWN){
mouseDown =false;
}
if(e.keyCode == Keyboard.LEFT){
mouseLeft =false;
}
if(e.keyCode == Keyboard.RIGHT){
mouseRight =false;
}
}
private function updatePos(e:Event){
if(mouseUp == true){
if(!wall.hitTestPoint(jacob.x,jacob.y-22, true)){
jacob.y -= speed;
}
}
if(mouseDown == true){
if(!wall.hitTestPoint(jacob.x, jacob.y+22, true)){
jacob.y += speed;
}
}
if(mouseLeft == true){
if(!wall.hitTestPoint(jacob.x-22, jacob.y, true)){
jacob.x -= speed;
}
}
if(mouseRight == true){
if(!wall.hitTestPoint(jacob.x+22, jacob.y, true)){
jacob.x += speed;
}
}
if(jacob.hitTestObject(a1)){
a1.x = a1.y = -1000;
_root.score = 0;
_root.score++;
score.text = _root.score;
}
if(jacob.hitTestObject(a2)){
a2.x = a2.y = -1000;
}
if(jacob.hitTestObject(a3)){
a3.x = a3.y = -1000;
}
if(jacob.hitTestObject(a4)){
a4.x = a4.y = -1000;
}
}
}
}

Flash Game Actionscript-3 Output Error #1009

I am making my own game using Actionscript 3 for coding in Flash. The game is about a Runner Character just like Super Mario and Stickyman, who just run, jump, dies.. and so on..
After being very organized by dividing each window of the game in a different Scene, I was still having the problem of executing the piece of code "gotoAndStop()".. That's why I decided to make it much simple by using only one Scene, but each wind of the game in a different frame in the Main Timeline. So I worked on the root, but still getting the same problem that stuck in a white screen!
Anyways, one of the main problem that I am facing here is the Error #1009. Even when I tried to read other topics with the same issue but I didn't find the answer that suites my case.
This is the Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at HR4_fla::MainTimeline/movePlayer()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Apple/update()
Here is the Code:
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.geom.Rectangle;
stop();
var KeyThatIsPressed:uint;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var score:int = 0;
var lives:int = 3;
player_mc.health = 100;
player_mc.dead = false;
//var TouchRestartBox:Boolean = false;
var playerSpeed:Number = 8;
var gravity:Number = 2;
var yVelocity:Number = 0;
var canJump:Boolean = false;
var canDoubleJump: Boolean = false;
//var appleCount:int;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.addEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
function cameraFollowCharacter(event:Event):void
{
scrollRect = new Rectangle(player_mc.x - stage.stageWidth/2, player_mc.y - stage.stageHeight/2, stage.stageWidth, stage.stageHeight);
}
//PressKey function here
function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = true;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = true;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = true;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = true;
}
}
//ReleaseKey function here
function ReleaseAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = false;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = false;
}
}
//stage.addEventListener(Event.ENTER_FRAME, GameOver);
stage.addEventListener(Event.ENTER_FRAME, movePlayer);
function movePlayer(event:Event):void
{
if(!rightKeyIsDown && !leftKeyIsDown && !upKeyIsDown)
{
player_mc.gotoAndStop(1);
}
if(rightKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x+= playerSpeed;
player_mc.scaleX = 0.59;
}
if(leftKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x-= playerSpeed;
player_mc.scaleX = -0.59;
}
if(upKeyIsDown && canJump)
{
player_mc.gotoAndStop(3);
yVelocity = -15;
canJump = false;
canDoubleJump = true;
}
if(upKeyIsDown && canDoubleJump && yVelocity > -2)
{
yVelocity = -13;
canDoubleJump = false;
}
yVelocity +=gravity;
if(!floor_mc.hitTestPoint(player_mc.x,player_mc.y, true))
{
player_mc.y+=yVelocity;
}
if(yVelocity > 20)
{
yVelocity =20;
}
for(var i:int=0; i<10; i++)
{
if(floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y--;
yVelocity = 0;
canJump = true;
}
}
for(var j:int=0; j<=2; j++)
{
if(rb.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.x = -1703.35;
player_mc.y = 322.1;
player_mc.scaleX = 0.59;
lives = lives - 1;
}
if(lives == 0)
{
// GameOver();
// remove all the event listeners
// stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
gotoAndStop(124);
//(root as MovieClip).gotoAndStop(124);
}
}
// appleCount_txt.text = "Apples:" + appleCount;
}
/*function GameOver()
{
// lives = 3;
// remove all the event listeners
stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
// player_mc.stop();
gotoAndStop(1); // this has your "dead" screen on it.
}*/
Can anyone help me solving this problem please?
Thanks
There's a listener still getting executed after the frame change. In that handler, an object is referenced that does not exist on that frame and is thus null.
Changing frames only provides visual changes, not necessarily a complete change of state.

AS3 error 1084 syntax error expecting rightparen before dot

i am new to this whole as3 thing and i am struggling immensely. I have been sat for the last two days trying to do something i can imaging to be simple to everyone else reading this. I am trying to create a game where i have a skate boarder controlled by the keyboard keys. However when i type this code in i am getting a 1084 error please help before i throw my laptop out the window. Thanks!!
package {
import flash.display.*;
import flash.events.*;enter code here
public class skatefate extends MovieClip {
var the_skater:Sprite = new Sprite();
the_skater.addChild:(skater);
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveskater);
function keyPressedDown(event:KeyboardEvent) {
if (event.keyCode == 37) {
moveLeft = true;
} else if (event.keyCode == 39) {
moveRight = true;
} else if (event.keyCode == 65) {
moveUp = true;
} else if (event.keyCode == 90) {
moveDown = true;
}
}
function keyPressedUp(event:KeyboardEvent) {
if (event.keyCode == 37) {
moveLeft = false;
} else if (event.keyCode == 39) {
moveRight = false;
} else if (event.keyCode == 65) {
moveUp = false;
} else if (event.keyCode == 90) {
moveDown = false;
}
}
function moveskater(event:Event) {
var speed:uint = 20;
if (moveLeft) {
skater.x -= speed;
if (skater.x < 0){
skater.x = 800;
}
}
}
if (moveRight) {
skater.x += speed;
if (skater.x > 800){
skater.x = 0;
}
}
if (moveUp) {
skater.y -= speed;
if (skater.y > 0){
skater.y = 0;
}
}
if (moveDown) {
skater.y += speed;
if (skater.y > 0){
skater.y = 0;
}
}
I tried your code but didn't get your error. Your sample throws other errors & problems.
So my advice is these two things..
The correct to construct your code...
package
{
//IMPORTS go here
//Declare your Class
public class skatefate extends MovieClip
{
//VARS go here
//*******************************************************************
//note: later you may also add other VARS inside functions as needed
//(but were not originally put (declared) in this section)
//*******************************************************************
//Declare main function of your Class (must have same name as Class (.as)
public function skatefate()
{
//Constructor code here
//************************************************************************
// Your main program code and related functions (K/board etc) go here and
// will reference your VARS declared above in public Class construction)
//************************************************************************
} //End of (public) Function
} //End of (public) Class
} //End of Package
Just incase you still struggle, this edit of your code shown should compile. From there you can study & learn. Hopefully one more laptop will survive in this cruel world.
package
{
import flash.display.*;
import flash.events.*; //enter code here
//Declare your Class
public class skatefate extends MovieClip {
var the_skater:Sprite = new Sprite();
var skater:Sprite = new Sprite(); //hide line if skater exists already (i.e in Library)
var speed:uint = 20;
//Declare main function of your Class
public function skatefate ()
{
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveskater);
the_skater.addChild(skater);
addChild(the_skater); //adds to stage
function keyPressedDown (event:KeyboardEvent)
{
if (event.keyCode == 37) { moveLeft = true; }
else if (event.keyCode == 39) { moveRight = true; }
else if (event.keyCode == 65) { moveUp = true; }
else if (event.keyCode == 90) { moveDown = true; }
}
function keyPressedUp (event:KeyboardEvent)
{
if (event.keyCode == 37) { moveLeft = false; }
else if (event.keyCode == 39) { moveRight = false; }
else if (event.keyCode == 65) { moveUp = false; }
else if (event.keyCode == 90) { moveDown = false; }
}
function moveskater(event:Event)
{
//var speed:uint = 20; //already declared at top
//speed = 20; // later change 'speed' this way by updating number
if (moveLeft) {
skater.x -= speed;
if (skater.x < 0)
{ skater.x = 800; }
}
if (moveRight) {
skater.x += speed;
if (skater.x > 800)
{ skater.x = 0; }
}
if (moveUp) {
skater.y -= speed;
if (skater.y > 0)
{ skater.y = 0; }
}
if (moveDown) { skater.y += speed;
if (skater.y > 0)
{ skater.y = 0; }
}
} //close 'moveskater' function
} //End of your (public) Function
} //End of your (public) Class
} //End of Package
Hope it helps. Ask for advice in the comments and don't forget to tick as "correct answer" if it works for you. That's how we say "Thanks" on Stack Overflow.

Null object error; cannot access it - AS3

I'm making a platform game. Im trying just to get the player to move around on the stage, and to be able to jump, with some type of gravity added to it. However, when I run it, I get this error: Error #1009: Cannot access a property or method of a null object reference.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Code1 extends MovieClip {
var charSpeed:int = 0;
var velocity:int = 0;
var gravity:Number = 1;
var Jump:Boolean = false;
var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
private var platform:Platform;
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function Code() {
}
public function update(evt:Event){
moveChar();
}
public function moveChar(){
if (leftKey == true){
charSpeed -= 10;
}
if (rightKey == true){
charSpeed += 10;
}
if (upKey == true){
if(!Jump){
velocity -= 14;
Jump = true;
}
}
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = true;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = true;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = true;
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = false;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = false;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = false;
}
}
function loop(evt:Event){
player.x = charSpeed;
if (player.x < 0){
player.x = 0;
}
if (player.x > 550){
player.x = 550;
}
velocity += gravity;
if (!platform.hitTestPoint(player.x, player.y, true)){
player.y += velocity;
}
for (var i = 0; i < 10; i++){
if (platform.hitTestPoint(player.x, player.y, true)){
player.y--;
velocity = 0;
Jump = false;
}
}
}
}
}
My platform linkage is "Platform", but i set up a variable for it (or tried to). I debugged the code, and it came up with this line: player.x = charSpeed;
I have no idea what to do, if someone could help, that would be great.
You never declare or instantiate (ie player = new Player()) your player.
Alternatively, if your player is on the stage of your .fla timeline, it will need the instance name 'player'. That can be set in the object properties.
You player object is null.
I don't see the istantiation line as:
var player:Player = new Player();
Add it before use a property of your player

Actionscript 3 returns Error #1009

I am creating a shooting game. I have encountered a problem with my code when I decided to create a method to remove the missile after reaching the top of the stage. I can run the program without any issues only I have realize that the missile was not remove away from the stage, if I hold the shooting button. However, if I tap the shooting button, the missile will removed away with this error #1009 printing out of the output.
Is there any solution to fix the problem?
Here's is the error after the missile flew to the top of the stage with debugging enabled:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Missle/destroyMissle()[E:\Experiment\ExperimentProject\Missle.as:39]
at main/checkMissleOffScreen()[E:\Experiment\ExperimentProject\main.as:63]
at main/eventUpdated()[E:\Experiment\ExperimentProject\main.as:51]
Here's the main's class:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
/**
* ...
* #author test
*/
public class main extends MovieClip
{
//Objects
public var rect:MovieClip;
public var missle:Missle;
//Array
private var missleArray:Array;
//Keyboard section
var leftKeyIsDown:Boolean;
var rightKeyIsDown:Boolean;
var upKeyIsDown:Boolean;
var downKeyIsDown:Boolean;
var spaceKeyIsDown:Boolean;
//Speed
var characterSpeed:Number = 15;
//Main constructor
public function main()
{
//Array initializer
missleArray = new Array();
missle = new Missle();
//Update events listeners.
stage.addEventListener(Event.ENTER_FRAME, eventUpdated);
//Update keyboard events listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUnpressed);
}
//Events functions
//This functions updated everytime the object is move
private function eventUpdated(e:Event):void
{
playerMoving();
playerClampMoving();
checkMissleOffScreen();
}
private function checkMissleOffScreen():void
{
for (var i = 0; i < missleArray.length; i++ )
{
var currentMissle:Missle = missleArray[i];
if (currentMissle.y < 0)
{
missleArray.splice(i, 1);
missle.destroyMissle();
}
}
}
private function playerClampMoving():void
{
if (rect.x < 0)
{
rect.x = 0;
}
if (rect.x > stage.stageWidth - rect.width)
{
rect.x = stage.stageWidth - rect.width;
}
if (rect.y < 0)
{
rect.y = 0;
}
if (rect.y > stage.stageHeight - rect.height)
{
rect.y = stage.stageHeight - rect.height;
}
}
private function playerMoving():void
{
if (leftKeyIsDown == true)
{
rect.x -= characterSpeed;
}
if (rightKeyIsDown == true)
{
rect.x += characterSpeed;
}
if (upKeyIsDown == true)
{
rect.y -= characterSpeed;
}
if (downKeyIsDown == true)
{
rect.y += characterSpeed;
}
if (spaceKeyIsDown == true)
{
shootingMissle();
}
}
private function shootingMissle():void
{
missle = new Missle();
missle.x = rect.x + (rect.width / 2);
missle.y = rect.y;
missleArray.push(missle);
trace(missleArray.length);
stage.addChild(missle);
}
//Keyboard functions
//Check to see whether the user releases the keyboard
private function keyUnpressed(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftKeyIsDown = false;
}
if (e.keyCode == 39)
{
rightKeyIsDown = false;
}
if (e.keyCode == 40)
{
downKeyIsDown = false;
}
if (e.keyCode == 38)
{
upKeyIsDown = false;
}
if (e.keyCode == 32)
{
spaceKeyIsDown = false;
}
}
//Check to see whether the user presses the keyboard
private function keyPressed(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftKeyIsDown = true;
}
if (e.keyCode == 39)
{
rightKeyIsDown = true;
}
if (e.keyCode == 40)
{
downKeyIsDown = true;
}
if (e.keyCode == 38)
{
upKeyIsDown = true;
}
if (e.keyCode == 32)
{
spaceKeyIsDown = true;
}
}
}
}
Here's the Missle's Class:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author test
*/
public class Missle extends Sprite
{
public function Missle()
{
addEventListener(Event.ADDED_TO_STAGE, onAdd);
}
private function onAdd(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAdd);
//Objects are on the stage
init();
}
private function init():void
{
addEventListener(Event.ENTER_FRAME, missleLaunch);
}
private function missleLaunch(e:Event):void
{
this.y -= 15;
}
public function destroyMissle():void
{
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, missleLaunch);
}
}
}
Try this:
public function destroyMissle():void
{
if(parent !== null) parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, missleLaunch);
}
It is a possibility that you were calling .destroyMissile() more than once meaning parent would be null because you've removed it from the stage and it has no parent.