Projectiles not moving as they are supposed to - actionscript-3

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
public class supportForce extends MovieClip
{
public function supportForce()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(Event.ENTER_FRAME, keyCheck);
}
private function onUp(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = true;
}
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
}
GreenLight.x -= defaultSpeed;
}
}
}
So the projectile here is Projectile1 and it is declared as GreenLight in the keyCheck Function. So in the same function (the ENTER.FRAME function) GreenLight is supposed to move to its left. This does not happen and also an error saying TypeError: Error #1009: Cannot access a property or method of a null object reference.
at supportForce/keyCheck() appears. Thank you in advance.

In your keyCheck function, GreenLight will be null if _keyDownStatus[90] is false
so put the GreenLight as a class variable
private var lights:Array = [];
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
for each (var l:Projectile1 in lights)
{
l.x -= defaultSpeed;
}
lights.push(GreenLight);
}
else
{
//remove all Projectile1s in lights from stage and clear lights if you want
}
}

you create GreenLight in if condition scope and if keyCode isnot 90, object of Projectile1 isnot created and GreenLight.x -= defaultSpeed; there GreenLight is null

Related

Movement Keys not Working

Nothing is happening when I press the arrow keys, but neither are there any errors: what's wrong with this? If I remove the key press testing it accelerates accordingly...
At this stage I am just trying to move a block around a screen in an inertial manner using the arrow keys. However, this is my first foray into AS3 so I may be going about it in completely the wrong manner.
Any help would be greatly appreciated.
Unit.AS:
package {
import flash.display.MovieClip;
import flash.events.*
import flash.ui.Keyboard
public class Unit extends MovieClip {
var velocityX:Number = 1;
var velocityY:Number = 1;
var accellerationX:Number = 1;
var accellerationY:Number = 1;
public function Unit(){
addEventListener("enterFrame", move);
}
private function move(e:Event){
accellerate()
this.x += velocityX;
this.y += velocityY;
}
private function accellerate(){
if (Key.isDown(Keyboard.UP)){
velocityY += accellerationY;
trace("Accellerating");
}
if (Key.isDown(Keyboard.DOWN)){
velocityY -= accellerationY;
trace("Accellerating");
}
if (Key.isDown(Keyboard.RIGHT)){
velocityX += accellerationX;
trace("Accellerating");
}
if (Key.isDown(Keyboard.LEFT)){
velocityX -= accellerationX;
trace("Accellerating");
}
}
}
}
Key.AS:
package
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Key {
private static var initialized:Boolean = false;
private static var keysDown:Object = new Object();
public static function initialize(stage:Stage) {
if (!initialized) {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
initialized = true;
}
}
public static function isDown(keyCode:uint):Boolean
{
return Boolean(keyCode in keysDown);
}
private static function keyPressed(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
private static function keyReleased(event:KeyboardEvent):void {
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
private static function clearKeys(event:Event):void {
keysDown = new Object();
}
}
}
On your unit constructor function call the initialize(stage) static function.
Key.initialize(stage);

How do I get my movieclip character to move?

I've been trying for a few hours now and I cant get my little character to move with the keyboard.
I have ran a trace to make see if anything was happening and the position value does change but my character doesn't react to that position change.
I receive no errors. Both my Character and BrickBlock are movieclips and they have been imported for ActionScript.
If any other information is needed please let me know. Thank you! :)
My following code:
package {
import flash.events.Event
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class CharMove extends MovieClip {
var char1 :Character;
var block :BrickBlock;
public function CharMove()
{
char1 = new Character();
block = new BrickBlock();
//this.addEventListener(Event.ENTER_FRAME, collide)
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
}
/*function collide(e:Event):void
{
if(char.hitTestObject(block))
{
char.visible = !char.visible;
}
}*/
function kDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
char1.x -= 5;
trace(char1.x);
break;
case Keyboard.RIGHT:
char1.x +=5;
trace(char1.x);
break;
}
}
}
}
You might want to consider writing a static Input class.
package input {
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class Input {
private static var keys:Array = new Array(255);
public static function setup(stage:Stage):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
}
private static function keyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
private static function keyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
}
public static function isKeyDown(k:int):Boolean {
return keys[k];
}
public static const A:uint = 65;
public static const B:uint = 66;
public static const C:uint = 67;
// The rest of the keys...
}
}
To use it first call setup() which adds the listeners for KEY_DOWN and KEY_UP events.
They you can easily query keys and do relevant actions accordingly.
Input.setup(stage);
/...
if(Input.isKeyDown(Input.A)) {
char1.x -= 5;
}

Error 1009 : Cannot access a property of method of a null object reference

I don't understand what is going on
This is my Main.as
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var pirkles:Circles = new Circles()
public function Main() {
gotoAndStop(1)
playbtn.addEventListener(MouseEvent.CLICK, playscreen)
}
public function playscreen(event:MouseEvent):void {
gotoAndStop(2)
addChild(pirkles)
}
}
}
And this is my Circles.as
package {
import flash.display.MovieClip
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
import flash.events.MouseEvent;
public class Circles extends MovieClip{
public function Circles():void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
this.y = 175
this.x = 10
}
public function MOVE(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
this.x = this.x+10
}
else if (event.keyCode == Keyboard.LEFT) {
this.x = this.x-10
}
else if (event.keyCode == Keyboard.UP) {
this.y = this.y-10
}
else if (event.keyCode == Keyboard.DOWN) {
this.y = this.y+10
}
}
}
}
Now I get an error that says there is a problem at line 11 of my Circles.as and at line 8 of my Main.as. However, at those lines I don't understand what is causing the problem. I added a event listener at line 11, but when I take it out it works. Also, at line 8, I Just defined a variable.
you can't access the stage in a class constructor.
So the line
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
is causing the error.
If you need to access the stage, add a listener in the constructor for the ADDED_TO_STAGE event, and in the callback function you will be able to access the stage
So:
public function Circles():void {
this.addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
this.y = 175
this.x = 10
}
private function onAddedToStage (evt:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
}

AS3 TypeError: Error #2007: Parameter hitTestObject must be non-null

TypeError: Error #2007: Parameter hitTestObject must be non-null.
I am working on a game right now and trying to get it so the player will collide with the ground (he already collides with table)
It says the problem is happening in playerOneCollisions
table and ground are just classes with the basic class code linked to movieclips,
they are extended movieclips (if that matters), and they have imported stage and movieclip (again, if that matters)
(the parts I think are important)
Player.as
public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;
public function playerOneCollisions(table, ground)
{
if(this.hitTestObject(table))
{
trace("tabled");
animation = "kong";
}
if(this.hitTestObject(ground))
{
trace("grounded");
animation = "idle";
}
}
The function playerOneCollisions is called in Main.as
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
The rest of the code incase I was wrong about the important parts
Player.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import KeyObject;
import Table;
public class Player extends MovieClip
{
public var stageRef:Stage;
public var key:KeyObject;
public var main:Main;
public var table:Table;
public var player:Player;
public var ground:Ground;
public var leftPressed:Boolean = false;
public var rightPressed:Boolean = false;
public var upPressed:Boolean = false;
public var downPressed:Boolean = false;
public var grounded:Boolean = false;
public var animation:String = "idle";
public var runSpeed:Number = 5;
public var animationState:String = "idle";
public function Player (stageRef:Stage, X:int, Y:int):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
key = new KeyObject(stageRef);
}
public function playerOnePress()
{
if(key.isDown(37) || key.isDown(65)){ // if left arrow or A is pressed
leftPressed = true;
//trace("left pressed");
} else {
leftPressed = false;
}
if(key.isDown(38) || key.isDown(87)){ // if up arrow or W is pressed
upPressed = true;
//trace("up pressed");
} else {
upPressed = false;
}
if(key.isDown(39) || key.isDown(68)){ //if right arrow or D is pressed
rightPressed = true;
//trace("right pressed");
} else {
rightPressed = false;
}
if(key.isDown(40) || key.isDown(83)){ //if down arrow or S is pressed
downPressed = true;
//trace("down pressed");
} else {
downPressed = false;
}
}
public function playerOneKeyResults()
{
if(leftPressed)
{
// trace("left");
this.x -= runSpeed;
}else if(rightPressed)
{
// trace("right");
this.x += runSpeed;
}
}
public function playerOneCollisions(table, ground)
{
if(this.hitTestObject(table))
{
trace("tabled");
animation = "kong";
}
if(this.hitTestObject(ground))
{
trace("grounded");
animation = "idle";
}
}
public function playerAnimaitons()
{
if(animation == "kong")
{
this.gotoAndStop(2);
}
if(animation == "idle")
{
this.gotoAndStop(1);
}
}
}
}
Main.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends MovieClip
{
public var gameTimer:Timer;
public var player:Player;
public var table:Table;
public var ground:Ground;
public function Main():void
{
gameTimer = new Timer (30);
gameTimer.addEventListener(TimerEvent.TIMER, GameClock);
gameTimer.start();
player = new Player(stage, 80, 420);
stage.addChild(player);
}
public function GameClock(timerEvent:TimerEvent):void
{
player.playerOnePress();
player.playerOneKeyResults();
player.playerOneCollisions(table, ground);
player.playerAnimaitons();
}
}
}
One thing I notice is that you have table and ground as class properties of Player, and they are also parameters for the method you are having trouble with.
The table and/or ground properties of your Player class are likely null as I don't see code that sets them.
It's not good practice to have method parameters named the same as your class properties.
Try removing the properties in your Player class. (I don't see them being set or used)
If you still get the error, trace out their values at the beginning of the playerOneCollisions method and ensure they are not null.
If they are null, the problem exists outside this class and you are passing in a null value.
I should also note that I don't see anywhere in the Main class where you set the table or ground properties, so they are null unless you have not shown us all the code. If they are null, then that will certainly cause the issue you are experiencing.

1180: Call to a possibly undefined method FlipUntilHead

package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.MouseEvent;
public class coinFlip extends MovieClip {// class
var bool:Boolean;
var num1:Number;
var counter:int;
public function coinFlip() {// constructor
RandomTruth();
var textF1:TextField = new TextField();
FlipUntilHead(RandomTruth); // the error is right here*
if (bool == false) {
textF1.text = "tails";
} else {
textF1.text = "heads";
}
addChild(textF1);
function RandomTruth() {
num1 = Math.random();
//trace(num1);
if (num1 < 0.5) {
bool = false;
return;
} else {
bool = true;
return;
}
function FlipUntilHead(RandomTruth) {
while (bool == false) {
RandomTruth();
counter ++;
return
}
//trace(counter);
}
}
}// end of constructor
}// end of class
}// end of package (program)
The program is supposed to "flip" a coin until it lands on heads using Math.random and boolean variables to declare if its "heads" or "tails".
I think it might be a very simple error and I am just dumb (beginner in programming).
The error is:
"1180: Call to a possibly undefined method FlipUntilHead"
Problem 1: You do not have to pass any parameters into your FlipUntilHead() function, because that function already calls RandomTruth().
Instead of function FlipUntilHead(RandomTruth) use function FlipUntilHead().
Then, instead of
RandomTruth();
var textF1:TextField = new TextField();
FlipUntilHead(RandomTruth);
use
var textF1:TextField = new TextField();
FlipUntilHead();
Although the function name indeed existed, the error was being thrown because it didn't understand the way that you were passing RandomTruth into it.
Problem 2: Also, there should be a } just before function FlipUntilHead(){, and one less } at the end of your program.
Problem 3: Finally, the while loop within FlipUntilHead() is not looping properly, because there is a return statement that immediately ends the function at the first loop. Here is the final code:
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.MouseEvent;
public class coinFlip extends MovieClip {
var bool:Boolean;
var num1:Number;
var counter:int;
public function coinFlip() {
var textF1:TextField = new TextField();
FlipUntilHead();
if (bool == false) {
textF1.text = "tails";
} else {
textF1.text = "heads";
}
addChild(textF1);
function RandomTruth() {
num1 = Math.random();
if (num1 < 0.5) {
bool = false;
return;
} else {
bool = true;
return;
}
}
function FlipUntilHead() {
while (bool == false) {
RandomTruth();
counter++;
}
}
}
}
}