PDA

View Full Version : [AS3] Pinball game error


amylou_184
03-25-2010, 01:27 AM
Hi I'm trying to make a pinball game and there is an error that keeps appearing. Below is the code I am using:
//
package
{
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.ui.*;

public class BouncingObject extends Sprite
{
private var Mxpos:Number;
private var Mypos:Number;
private var Mxvel:Number;
private var Myvel:Number;
private var Mgrav:Number;
private var Mdirection_D:Number;
private var Mdirection_R:Number;

public function BouncingObject(Pxpos:Number, Pypos:Number, Pxvel:Number, Pyvel:Number, Pgrav:Number)
{
//store parameters
Mxpos = Pxpos;
Mypos = Pypos;
Mxvel = Pxvel;
Myvel = Pyvel;
Mgrav = Pgrav;

var Ball:Sprite = new Ball1();
addChild(Ball);

//set current position
x = Mxpos;
y = Mypos;

//other things to load once the stage is up and running
addEventListener(Event.ADDED_TO_STAGE, loadComplete, false, 0, true);
}

public function loadComplete(evt:Event):void
{
var Hippo1:Sprite = new HippoObject1();
parent.addChild(Hippo1);
//Place in the middle
Hippo1.x=275;
Hippo1.y=400;

//Add frame listener
addEventListener(Event.ENTER_FRAME, onRun, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_DOWN, BallLauncher);

}

public function BallLauncher (event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
Myvel= -50;
}
}

//Deal with a collision with a horizontal surface
private function doCollisionH():void
{
//Get current angle
Mdirection_R = Math.atan2 (Mypos-y, Mxpos -x);

//Convert to degrees
Mdirection_D = Mdirection_R * (180/Math.PI);


//Check angle is a +ve number
if (Mdirection_D < 0)
{
Mdirection_D += 360;
}

//Get new angle
Mdirection_D = (180-Mdirection_D)+180;



//Check angle is a +ve number
if (Mdirection_D < 0)
{
Mdirection_D += 360;
}

//Get speed along angle
var speed:Number = Math.sqrt ((Mxvel*Mxvel)+(Myvel*Myvel));

//Apply friction?
speed*= 0.9;

//Convert degrees to radians
Mdirection_R = (Math.PI/180) * Mdirection_D;

//Set Mxvel and Myvel speed
Mxvel = speed*Math.cos (Mdirection_R);
Myvel = speed*Math.sin (Mdirection_R);
}

//Deal with a collision with a vertical surface
private function doCollisionV():void
{
//Get current angle
Mdirection_R = Math.atan2 (Mypos-y, Mxpos -x);

//Convert to degrees
Mdirection_D = Mdirection_R * (180/Math.PI);

//Check angle is a +ve number
if (Mdirection_D < 0)
{
Mdirection_D += 360;
}

//Get new angle
Mdirection_D =(180-Mdirection_D);

//Check angle is a +ve number
if (Mdirection_D < 0)
{
Mdirection_D += 360;
}

//Get speed along angle
var speed:Number = Math.sqrt ((Mxvel*Mxvel)+(Myvel*Myvel));

//Apply friction?
speed*= 0.9;

//Convert degrees to radians
Mdirection_R = (Math.PI/180) * Mdirection_D;

//Set Mxvel and Myvel speed
Mxvel = speed*Math.cos (Mdirection_R);
Myvel = speed*Math.sin (Mdirection_R);
}

private function onRun(evt:Event):void
{
//Increase Y velocity by gravity
Myvel += Mgrav;
//Increase X position by X velocity
Mxpos += Mxvel;
//Increase Y position by Y velocity
Mypos += Myvel;

//Check for collision with stage
//Hitting Bottom
if (Mypos+height > stage.stageHeight)
{
//React to collision
doCollisionH();


//Change ball position
Mypos = stage.stageHeight - height;
y = Mypos;

}

//Hitting Top
else if (Mypos < 0)
{
//React to collision
doCollisionH();


//Change ball position
Mypos = 0;
y = Mypos;

}

//Hitting Right
else if (Mxpos+width > stage.stageWidth)
{
//React to collision
doCollisionV();

//Change ball position
Mxpos = stage.stageWidth - width;
x = Mxpos;

}

//Hitting Left
else if (Mxpos < 0)
{
//React to collision
doCollisionV();

//Change ball position
Mxpos = 0;
x = Mxpos;

}
if (y+height<HippoObject1.y||y>HippoObject1.y+HippoObject1.height)
{
var topDistanceb:int= Math.abs(HippoObject1.y - (y+height));
var bottomDistanceb:int=Math.abs(HippoObject1.y+HippoO bject1.height-y);

if (topDistanceb<bottomDistanceb) {
doCollisionH();
Mypos=HippoObject1.y-height;
y=Mypos;
}
else {
doCollisionH();
Mypos=HippoObject1.y+HippoObject1.height;
y=Mypos;
}

}
else if (x+width<HippoObject1.x||x>HippoObject1.x+HippoObject1.width) {
var leftDistanceb:int= Math.abs(HippoObject1.x - (x+width));
var rightDistanceb:int=Math.abs(HippoObject1.x+HippoOb ject1.width-x);

if (leftDistanceb<rightDistanceb)
{
doCollisionV();
Mxpos=HippoObject1.x-width;
x=Mxpos;
}
else
{
doCollisionV();
Mxpos=HippoObject1.x+HippoObject1.width;
x=Mxpos;
}
}
if (Mdirection_D<0)
{
Mdirection_D+=360;
}
//Update position of ball (only done here when there are no collisions)
x = Mxpos;
y = Mypos;
}

//Collision with box

//Hitting top

//React to collision

//Change ball position


//Hitting bottom

//React to collision

//Change ball position

//Hitting left

//React to collision

//Change ball position

//hitting right

//React to collision

//Change ball position

}
}
The error I am getting is :
1119: Access of possibly undefined property y through a reference with static type Class.

Thanks in advance.

UncleNinja
03-25-2010, 01:48 AM
What line is that thrown on?
(Use debug to see - Control-Shift-Enter)

amylou_184
03-25-2010, 01:52 AM
The problems appear in here:
if (y+height<HippoObject1.y||y>HippoObject1.y+HippoObject1.height)
{
var topDistanceb:int= Math.abs(HippoObject1.y - (y+height));
var bottomDistanceb:int=Math.abs(HippoObject1.y+HippoO bject1.height-y);

if (topDistanceb<bottomDistanceb) {
doCollisionH();
Mypos=HippoObject1.y-height;
y=Mypos;
}
else {
doCollisionH();
Mypos=HippoObject1.y+HippoObject1.height;
y=Mypos;
}

}
else if (x+width<HippoObject1.x||x>HippoObject1.x+HippoObject1.width) {
var leftDistanceb:int= Math.abs(HippoObject1.x - (x+width));
var rightDistanceb:int=Math.abs(HippoObject1.x+HippoOb ject1.width-x);

if (leftDistanceb<rightDistanceb)
{
doCollisionV();
Mxpos=HippoObject1.x-width;
x=Mxpos;
}
else
{
doCollisionV();
Mxpos=HippoObject1.x+HippoObject1.width;