2D movement and hit testPoints - actionscript-3

I'm trying to make a character able to move within the "world" movieclip, but not go through the walls.
So here is the character class, pretty basic. The moving variable dictates whether the character will move or not.
The issue is, when I move the character left or right against the "world" the character will move upwards or downwards as well.
// Code to move character
package
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
public class Character extends MovieClip
{
public var Moving:Boolean = true;
public var maxSpeed = 10;
public var dx = 0;
public var dy = 0;
public var rot = 0;
public var rof = 30;
private var keysDown:Array = new Array ;
// Declare variables:
//var bullet:Bullet;
var reload:int = 10;
private var bullets:Array;
public function Character()
{
//bullets = bulletList;
// Initialize variables:
addEventListener(Event.ADDED_TO_STAGE,init);
}
function init(e:Event):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP,keyReleased);
this.addEventListener(Event.ENTER_FRAME,processInput);
this.addEventListener(Event.ENTER_FRAME,moveMe);
removeEventListener(Event.ADDED_TO_STAGE,init);
}
public function removeT():void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyReleased);
this.removeEventListener(Event.ENTER_FRAME,processInput);
this.removeEventListener(Event.ENTER_FRAME,moveMe);
}
private function keyPressed(e:KeyboardEvent):void
{
keysDown[e.keyCode] = true;
// Handle keys pressed:
}
private function keyReleased(e:KeyboardEvent):void
{
keysDown[e.keyCode] = false;
}
private function processInput(e:Event)
{
// Handle keys held:
dx = 0;
dy = 0;
if (keysDown[Keyboard.LEFT])
{
dx = - maxSpeed;
dy = 0;
rot = -180;
this.gotoAndStop(4);
}
if (keysDown[Keyboard.RIGHT])
{
dx = maxSpeed;
dy = 0;
rot = 0;
this.gotoAndStop(1);
}
if (keysDown[Keyboard.UP])
{
dx = 0;
dy = - maxSpeed;
rot = -90;
this.gotoAndStop(3);
}
if (keysDown[Keyboard.DOWN])
{
this.gotoAndStop(2);
dx = 0;
dy = maxSpeed;
rot = 90;
}
}
private function moveMe(e:Event)
{
if(Moving){
this.x += dx;
this.y += dy;
this.rotation = rot;
}
//stop if it tries to go off the screen
if (this.x < 0)
{
this.x = 0;
}
if (this.x > stage.stageWidth)
{
this.x = stage.stageWidth;
}
if (this.y < 0)
{
this.y = 0;
}
if (this.y > stage.stageHeight)
{
this.y = stage.stageHeight;
}
}
}
}
Here is the code for the collision checking of the object and the world. I use the functions checkFrom and checkFromX to check the points around the movieclip. So when one of those functions are checked, it iterates through all the points on that side of the movieclip character
picture of the movieclip character if anyone is confused. http://prntscr.com/5fqu69
function checkFrom(x1:int, x2:int, y:int )
{
if (x2<x1) {
trace("x2<x1");
return false;
}
for (var i=x1; i<=x2; i++)
{
if (world.hitTestPoint(i,y,true))
{
return true;
}
}
return false;
}
//made this to check for right and left
function checkFromX(y1:int, y2:int, x:int )
{//y1 at top
if (y2<y1) {
trace("y2<y1");
return false;
}
for (var a=y1; a<=y2; a++)
{
if (world.hitTestPoint(x,a,true))
{
return true;
}
}
return false;
}
function moveDude(e:Event):void
{
var obj:Object = e.target;
if (obj.hitTestObject(world.lv1))
{
cleanup();
gotoAndStop("donkeyKong");
}
else
{
obj.Moving = true;
while (checkFrom(obj.x - obj.width / 2 + obj.width / 8,obj.x + obj.width / 2 - obj.width / 8,obj.y + obj.height / 2 - obj.height / 9))
{//bot
obj.y--;
obj.Moving = false;
}
while (checkFrom(obj.x - obj.width / 2 + obj.width / 8,obj.x + obj.width / 2 - obj.width / 8,obj.y - obj.height / 2 + obj.height / 9))
{
obj.y++;
obj.Moving = false;
}
while (checkFromX(obj.y - obj.height / 2 + obj.height / 7,obj.y - obj.height / 7 + obj.height / 2,obj.x - obj.width / 2 + obj.width / 9))
{//left
obj.x++;
obj.Moving = false;
}
while (checkFromX(obj.y - obj.height / 2 + obj.height / 7,obj.y + obj.height / 2 - obj.height / 7,obj.x + obj.width / 2 - obj.width / 9))
{
obj.x--;
obj.Moving = false;
}

Related

In AS3 how would i add 2 buttons that would enable the player to move on press instead of using the arrow keys?

I got this code off the internet for the game Pong to work on my AS3 document for my assignment. However i'm pretty much a beginner at code and i'm trying to get this game to work on mobile as the assignment needs a game to work on it.
Because it uses arrow keys, i would like to basically just replace them with buttons instead, one for going up and one for going down. I just don't know the type of code that would allow me to do that.
Something like, when button is pressed, player moves up or down depending which button, but im not sure where to replace the code and what to get rid of.
Here's the "Pong" class file:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Pong extends MovieClip {
//constants
private var pongUp:MovieClip = new PongUp ;
private var pongDown:MovieClip = new PongDown ;
const ballspeed:int = 10;
const playerspeed:int = 7;
const computerspeed:int = 10;
const computerIntelligence:int = 7;//intelligence is 7 out of 10
//global variables
var vx:int = - ballspeed;// x component of velocity of ball (velocity is speed with direction)
var vy:int = ballspeed;// y component of velocity of ball
var v1:int = 0;// initial velocity of player
var v2:int = 0;// initial velocity of computer
var playerScore:int = 0;
var computerScore:int = 0;
var player:MovieClip = new PongPlayer ;
var computer:MovieClip = new PongComputer ;
var ball:MovieClip = new PongBall ;
public function Pong() {
//init();
addEventListener(Event.ADDED_TO_STAGE,init);
}
//this function will add all event listeners
function init(e:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
stage.addEventListener(Event.ENTER_FRAME,EnterFrame);
addChild(player);
addChild(computer);
addChild(ball);
player.x = 23;
player.y = 300;
computer.x = 637;
computer.y = 311;
ball.x = 308;
ball.y = 328;
addChild(pongUp);
pongUp.x = 25;
pongUp.y = 700;
addChild(pongDown);
pongDown.x = 530;
pongDown.y = 700;
}
// this function resets the game
function reset():void {
player.y = stage.stageHeight / 2;
computer.y = stage.stageHeight / 2;
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
if (Math.abs(Math.random() * 2) > 1)
{
vx = - ballspeed;
}
else
{
vx = ballspeed;
}
if (Math.abs(Math.random() * 2) > 1)
{
vy = - ballspeed;
}
else
{
vy = ballspeed;
}
}
//pongDown.addEventListener ( MouseEvent.MOUSE_DOWN,moveDown );
//function moveDown ( e:MouseEvent ): void
//{
//}
//this function sets the velocity of player when key is pressed
function KeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP)
{
v1 = - playerspeed;
}
else if (event.keyCode == Keyboard.DOWN)
{
v1 = playerspeed;
}
}
//this function sets the velocity of player to 0 if key is released
function KeyUp(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
{
v1 = 0;
}
}
//This function is executed when a frame changes
function EnterFrame(event:Event):void {
//variable decleration
var pHalfHeight = player.height / 2;// half height of player(used for collisions)
var pHalfWidth = player.width / 2;// half width of player (used for collisions)
var bHalfHeight = ball.height / 2;// half height of ball(used for collisions)
var bHalfWidth = ball.width / 2;// half width of ball (used for collisions)
//moving the player
player.y += v1;
//limiting the motion of player (it should not move beyond the stageheight)
if (player.y + pHalfHeight > stage.stageHeight)
{
player.y = stage.stageHeight - pHalfHeight;
}
else if (player.y - pHalfHeight < 0)
{
player.y = 0 + pHalfHeight;
}
//moving the ball
ball.x += vx;
ball.y += vy;
//moving the computer automatically
if (Math.abs(Math.random() * 10) < computerIntelligence)
{
var d:int = computer.y - ball.y;
if (Math.abs(d) > pHalfHeight)
{
if ((d > 0))
{
v2 = - computerspeed;
}
else
{
v2 = computerspeed;
}
}
}
computer.y += v2;
//limiting the motion of computer (it should not move beyond the stageheight)
if (computer.y + pHalfHeight > stage.stageHeight)
{
computer.y = stage.stageHeight - pHalfHeight;
}
else if (computer.y - pHalfHeight < 0)
{
computer.y = 0 + pHalfHeight;
}
//collision with horizontal walls
if (ball.y + bHalfHeight >= stage.stageHeight || ball.y - bHalfHeight <= 0)
{
vy *= -1;
}
//collision with player and computer
if (ball.x - bHalfWidth <= player.x + pHalfWidth)
{
if (Math.abs(ball.y - player.y) <= pHalfHeight)
{
vx = ballspeed;
if ((v1 != 0))
{
vy = 2 * v1;
}
}
}
else if (ball.x + bHalfWidth >= computer.x - pHalfWidth)
{
if (Math.abs(ball.y - computer.y) <= pHalfHeight)
{
vx = - ballspeed;
if ((v2 != 0))
{
vy = v2;
}
}
}
//collision with vertical walls & updating scores
if (ball.x + bHalfWidth >= stage.stageWidth)
{
playerScore += 1;
reset();
}
else if (ball.x - bHalfWidth <= 0)
{
computerScore += 1;
reset();
}
//display the score on the textfield
//txtPlayer.text = String(playerScore);
//txtComputer.text = String(computerScore);
}
}
}
For mobile, you'll want to compile using AIR for Android or iOS. You can then replace:
//this function sets the velocity of player when key is pressed
function KeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP)
{
v1 = - playerspeed;
}
else if (event.keyCode == Keyboard.DOWN)
{
v1 = playerspeed;
}
}
//this function sets the velocity of player to 0 if key is released
function KeyUp(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
{
v1 = 0;
}
}
with:
function KeyDown(event:TouchEvent):void {
if (event.stageY < player.y){
v1 = - playerspeed;
}else if (event.stageY > player.y){
v1 = playerspeed;
}
}
function KeyUp(event:TouchEvent):void {
v1 = 0;
}
Then replace:
//this function will add all event listeners
function init(e:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
with:
//this function will add all event listeners
function init(e:Event):void {
stage.addEventListener(TouchEvent.TOUCH_BEGIN, KeyDown);
stage.addEventListener(TouchEvent.TOUCH_END, KeyUp);
And make sure to use TouchEvent instead of KeyboardEvent by replacing:
import flash.events.KeyboardEvent;
with:
import flash.events.TouchEvent;
Now when you hold your finger down on the screen, the "player" will move towards that Y location. If your game is using the X axis, just change the Ys to Xs!
Hope this helps!

Access of possibly undefined property alpha through a reference with static type Class

I'm not sure how to go about setting the transparency when the character's health goes down.
Error is on line 38
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
public class Character extends MovieClip
{
var velocity:Number;
var shootLimiter:Number;
var health:Number;
var maxHealth:Number;
public function Character()
{
velocity = 10;
shootLimiter = 0;
health = 100;
maxHealth = 100;
addEventListener("enterFrame", move);
x = 300
y = 150
}
function takeDamage(d)
{
health -= d;
if(health <= 0)
{
health = 0;
kill();
}
Game.healthMeter.bar.scaleX = health/maxHealth;
Character.alpha = health/100;
}
function kill()
{
var blood = new Blood();
stage.addChild(blood);
blood.x = this.x;
blood.y = this.y;
removeEventListener("enterFrame",move);
this.visible = false;
Game.gameOver();
}
function move(e:Event)
{
shootLimiter += 5;
if(Key.isDown(Keyboard.RIGHT))
{
if(this.x <= 560)
{
this.x = this.x + velocity;
}
}
if(Key.isDown(Keyboard.LEFT))
{
if(this.x >= 40)
{
this.x = this.x - velocity;
}
}
if(Key.isDown(Keyboard.UP))
{
if(this.y > 20)
{
this.y = this.y - velocity;
}
}
if(Key.isDown(Keyboard.DOWN))
{
if(this.y < 280)
{
this.y = this.y + velocity;
}
}
if(Key.isDown(Keyboard.SPACE) && shootLimiter > 8)
{
shootLimiter = 0;
var b = new Needles();
stage.addChild(b);
b.x = this.x+65;
b.y = this.y+45;
}
}
}
}
Character has no static property called alpha. You are referring to the instance of the class and therefore it should be this.alpha = health/100; instead of Character.alpha = health/100;

How to implement enemy behaviour based on type?

I am building a game which create three types of enemy.Amount them only type 3 can fire others cannt.This is my enemy class
package
{
import flash.display.MovieClip;
import flash.utils.getTimer;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.Point;
public class Enemy extends MovieClip
{
private var lastTime:int;
var hitCounter:Number = 1;
public var eType:Number = 0;
private var startYpos:Number = 0;
var nextFire:Timer;
var enemyType:Number;
public var bullets:Array = new Array ;
var speedY:Number = 50;
var enemySpeed:Number = 50;
var firstPos:Number = 0;
var fireCounter:Number = 0;
var firePause:Number = 10;
public function Enemy(xPos,yPos:Number,t:Number)
{
// constructor code
this.x = xPos;
firstPos = this.y = yPos;
this.enemyType = t;
lastTime = getTimer();
this.gotoAndStop(t);
addEventListener(Event.ENTER_FRAME,moveEnemy);
}
public function moveEnemy(event:Event)
{
// get time passed
var timePassed:int = getTimer() - lastTime;
lastTime += timePassed;
// move bullet
if (this.y + this.height / 2 > firstPos + 100 && speedY > 0)
{
speedY *= -1;
}
if (this.y - this.height / 2 < firstPos && speedY < 0)
{
speedY *= -1;
}
this.x -= enemySpeed * timePassed / 1000;
this.y += speedY * timePassed / 1000;
// bullet past top of screen
if (this.x - this.width / 2 < 0)
{
deleteEnemy();
}
if (this.enemyType == 3)
{
canFire();
}
}
public function canFire()
{
if ((fireCounter > firePause))
{
MovieClip(parent).createEnemyBullet();
trace((('Enemy Type : ' + enemyType) + ' and firing'));
fireCounter = 0;
}
else
{
fireCounter++;
}
}
public function deleteEnemy()
{
if (this.currentFrame == 2)
{
this.gotoAndStop(4);
}
else
{
//trace(MovieClip(parent).enemyKilled[this.enemyType-1]);
MovieClip(parent).enemyKilled[this.enemyType - 1]++;
MovieClip(parent).removeEnemy(this);
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,moveEnemy);
}
}
}
}
Now once a enemy type 3 start firing then every enemy start firing.i want just only enemy type 3 can fire not other enemy.How i do it?
Thanks in advance
Addition :
public function createEnemyBullet()
{
var bullet:Bullet = new Bullet(enemy.x - 10,enemy.y,500,-1);
bullets.push(bullet);
addChild(bullet);
//setEnemyBullet();
}
Then why don't you try something like this. `
public function canFire()
{
if(enemyType == 3){
return;
}
if ((fireCounter > firePause))
{
MovieClip(parent).createEnemyBullet();
trace((('Enemy Type : ' + enemyType) + ' and firing'));
fireCounter = 0;
}
else
{
fireCounter++;
}
}
`

Adobe Flash Game Programming

I'm programming a ball game in Adobe Flash, Javascript 3 and I getting a error at line 113( the last line ) that says:
1087: Syntax error: extra characters found after end of program.
package
{
import flash.display.MovieClip
import flash.text.TextField
import flash.events.Event
import flash.events.MouseEvent
public class DocumentMain extends MovieClip
{
public const GRAVITY:Number = 2;
public const BOUNCE_FACTOR:Number = 0.8;
public var _bounces:TextField;
public var _highscore:TextField;
public var _ball:Ball;
private var _vx:Number;
private var _vy:Number;
public function DocumentMain():void
{
_vx = 10;
_vy = 0;
_ball.buttonMode = true;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
private function enterFrameHandler(e:Event):void
{
// gravitate the ball
_vy += GRAVITY;
// move the ball
_ball.x += _vx;
_ball.y += _vy;
// check boundaries for collusion
checkBoundaryCollisions();
}
private function mouseDownHandler(e:MouseEvent):void
{
//hit the ball if it has been clicked
if (e.target == _ball)
{
hit(e.target.mouseX, e.target.mouseY);
}
}
private function checkBoundaryCollisions():void
{
var left:Number;
var right:Number;
var bottom:Number;
var top:Number;
left = _ball.x - (_ball.width / 2);
right = _ball.x + (_ball.width / 2);
bottom = _ball.y + (_ball.height / 2);
top = _ball.y - (_ball.height / 2);
if (left < 0 && _vx < 0)
{
_ball.x = _ball.width / 2;
_vx *= -1;
}
else if (right > stage.stageWidth && _vx > 0)
{
_ball.x = stage.stageWidth - (_ball.width / 2);
_vx *= -1;
}
if (top < 0 && _vy < 0)
{
_ball.y = _ball.height / 2;
_vy *= -1;
}
else if (bottom > stage.stageHeight && _vy > 0)
{
_ball.y = stage.stageHeight - (_ball.height / 2);
_vy *= -BOUNCE_FACTOR;
_vx *= BOUNCE_FACTOR;
if (Number(_bounces.text) > Number(_highscore.text))
{
_highscore.text = _bounce.text;
}
_bounces.text = "0";
}
}
private function hit(hitX:Number, hitY:Number):void
{
//increment bounces
_bounces.text = String.(Number(_bounces.text) + 1);
//adjust the vertical velocity of the ball
if (_vy > 0)
{
_vy *= -BOUNCE_FACTOR / 2 ;
}
_vy -= HIT_FORCE;
//adjust the horizontaly velocity of the ball
if (_vx * hitX > 0)
{
_vx *= -BOUNCE_FACTOR;
}
_vx -= ( 2 * hitX / _ball.width) * HIT_FORCE;
}
}
}
}
get rid of the last curly brace }. You have too many at the end of the file.
I also think you mean Actionscript 3.

Flash - using multiple event handlers?

so I'm working on a game which uses multiple .as files - and I'm trying to program the game however it keeps kicking back the following error when it reaches line 21 in theGame.as - the error is...
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at theGame()[M:\Users\----\Documents\Programming For Fun\Flash\Tutorial - Menu System\theGame.as:21]
at main/changeState()[M:\Users\----\Documents\Programming For Fun\Flash\Tutorial - Menu System\main.as:23]
at mainMenu/brnP_Button()[M:\Users\----\Documents\Programming For Fun\Flash\Tutorial - Menu System\mainMenu.as:40]
Cannot display source code at this location.
I'm using the following .as files.
main.as
//main.as
package
{
import flash.display.*;
import flash.system.fscommand;
public class main extends MovieClip
{
public function main()
{
changeState(null, "menu");
}
public function changeState (currentState, nextState)
{
if (currentState != null)
{
removeChild(currentState);
}
switch(nextState)
{
case "menu": var mm:mainMenu = new mainMenu(changeState);
addChild(mm);
break;
case "game": var g:theGame = new theGame(changeState);
addChild(g);
break;
case "exit": fscommand("quit");
break;
}
}
}
}
mainMenu.as
`
//mainMenu.as
package
{
import flash.display.*;
import flash.events.*;
public class mainMenu extends MovieClip
{
var theCallBackFunction:Function;
private var ballX:Number = 10; // Declaring a variable known as ballX
private var ballY:Number = 0; // Declaring a variable knwon as ballY
private const GRAVITY:Number = 2; // Declaring a const variable known as gravity
public const ROTATION:Number = 1;
public const BOUNCE:Number = 0.9;
var gameBall:Ball = new Ball();
public function mainMenu(callBack)
{
addEventListener(Event.ENTER_FRAME, menuFrameHandler);
addChild(gameBall);
var btnPlay:mmPlay = new mmPlay();
btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, brnP_Button);
btnPlay.x = width/2;
btnPlay.y = height/2 - btnPlay.height/2;
addChild(btnPlay);
var btnExit:mmExit = new mmExit();
btnExit.addEventListener(MouseEvent.MOUSE_DOWN, brnE_Button);
btnExit.x = width/2;
btnExit.y = height/2 - btnExit.height/2;
btnExit.y += btnExit.height + 4;
addChild(btnExit);
theCallBackFunction = callBack;
}
public function brnP_Button(e:MouseEvent)
{
removeEventListener(Event.ENTER_FRAME, menuFrameHandler);
theCallBackFunction(this, "game");
return;
}
public function brnE_Button(e:MouseEvent)
{
theCallBackFunction(this, "exit");
return;
}
private function menuFrameHandler (e:Event):void
{
// Gravitate the Ball
ballY += GRAVITY; // The ball is effected by gravity each frame
// Move The Ball
gameBall.x += ballX;
gameBall.y += ballY;
gameBall.rotation += ROTATION * ballX;
checkBoundaryCollision();
}
private function checkBoundaryCollision():void
{
var left:Number;
var right:Number;
var bottom:Number;
left = gameBall.x - (gameBall.width / 2);
right = gameBall.x + (gameBall.width / 2);
bottom = gameBall.y;
if (left < 0 && ballX < 0)
{
gameBall.x = (gameBall.width / 2)
ballX *= -1;
}
else if (right > stage.stageWidth && ballX > 0)
{
gameBall.x = stage.stageWidth - (gameBall.width / 2)
ballX *= -1;
}
else if (bottom > stage.stageHeight && ballY > 0)
{
gameBall.y = stage.stageHeight - (gameBall.height/2)
ballY *= -BOUNCE;
}
}
}
}
theGame.as
//theGame.as
package
{
import flash.display.MovieClip
import flash.text.TextField
import flash.events.Event
import flash.events.MouseEvent
public class theGame extends MovieClip
{
public const GRAVITY:Number = 2; // Declaring a const variable known as gravity
public const BOUNCE:Number = 0.8;
public const HIT:Number = 15;
public const ROTATION:Number = 1;
public var gameBall:Ball;
private var ballX:Number; // Declaring a variable known as ballX
private var ballY:Number; // Declaring a variable knwon as ballY
public function theGame(callBack): void
{
addChild(gameBall);
ballX = Math.random(); // Initalising ballX
ballY = Math.random(); // Initalising ballY
gameBall.buttonMode = true;
addEventListener(Event.ENTER_FRAME, gameFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
private function gameFrameHandler (e:Event):void
{
// Gravitate the Ball
ballY += GRAVITY; // The ball is effected by gravity each frame
// Move The Ball
gameBall.x += ballX;
gameBall.y += ballY;
gameBall.rotation += ROTATION * ballX;
// Check Stage Boundaries For Collisions
checkBoundaryCollision();
}
private function mouseDownHandler (e:MouseEvent):void
{
// Hit the ball if it has been clicked
if (e.target == gameBall)
{
hit(e.target.mouseX, e.target.mouseY);
}
}
private function checkBoundaryCollision():void
{
var left:Number;
var right:Number;
var bottom:Number;
var top:Number;
left = gameBall.x - (gameBall.width / 2);
right = gameBall.x + (gameBall.width / 2);
bottom = gameBall.y + (gameBall.height / 2);
top = gameBall.y - (gameBall.height / 2);
if (left < 0 && ballX < 0)
{
gameBall.x = (gameBall.width / 2)
ballX *= -1;
}
else if (right > stage.stageWidth && ballX > 0)
{
gameBall.x = stage.stageWidth - (gameBall.width / 2)
ballX *= -1;
}
if (top < 42.70 && ballY < 0)
{
gameBall.y = 42.70 + (gameBall.height / 2)
ballY *= -1;
}
else if (bottom > stage.stageHeight && ballY >= 0)
{
gameBall.y = stage.stageHeight - (gameBall.height/2)
ballY *= -BOUNCE;
ballX *= BOUNCE;
}
}
private function hit(hitX:Number, hitY:Number):void
{
// Adjust vertical velocity
if (ballY > 0)
{
ballY *= -BOUNCE / 2;
}
ballY -= HIT;
//adjust horizontal veloity
if (ballX * hitX > 0)
{
ballX *= -BOUNCE;
}
ballX -= (hitX / gameBall.width * HIT);
}
}
}
How would I go about fixing this?
You don't have an instance of gameBall inside your class theGame.
Try adding
gameBall = new Ball();
before
addChild(gameBall);