Game is pretty much complete, but I am having a problem that is driving me nuts. At the end of a game, I am trying to give an option to start the game over again. The problem is I am having to try to do this from a different object than the start of the game is located.
Here is the code for my two classes, I’ve added a comment on the part that is giving me the problems.
Main.as:
package
{
// Imports \\
import fl.transitions.Tween;
import flash.display.MovieClip;
import flash.events.Event;
import com.greensock.TweenLite;
import fl.motion.easing.Bounce;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends MovieClip {
public function begin() : void {
// rotates stage 30º
bg.rotationX = -30;
// listens for mouse to press "Click here to start"
Mouse.show ();
startText.addEventListener (MouseEvent.MOUSE_UP, gameStart );
startText.visible = true;
startText.buttonMode = true;
}// end function
public function gameStart ( _e : Event ) : void {
// turns button off and hides the mouse
startText.buttonMode = false;
startText.visible = false;
Mouse.hide ();
// rotates the walls onto stage 90º
new TweenLite( bg.wallRight, 2, { rotationY: 90, alpha: 1, ease: Bounce.easeOut } );
new TweenLite( bg.wallLeft, 2, { rotationY: -90, alpha: 1, ease: Bounce.easeOut } );
new TweenLite( bg.wallTop, 2, { rotationX: 90, alpha: 1, ease: Bounce.easeOut } );
new TweenLite( bg.wallBottom, 2, { rotationX: -90, alpha: 1, ease: Bounce.easeOut, onComplete: ball.drop } );
}// end function
}// end class
}// end package
Ball.as:
package {
// Imports \\
import flash.display.MovieClip;
import flash.events.Event;
import com.greensock.TweenLite;
import fl.motion.easing.Bounce;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Ball extends MovieClip {
// declares ball variables
public var xspeed:Number=0;
public var yspeed:Number=0;
// declares fScore score variable
public var fscore:Number=0;
public var scoreRight:Number = 0;
public var scoreLeft:Number = 0;
public var maxScore:Number = 2;
public function Ball():void {
}
public function drop():void {
// stops ball's movement and removes the moveBall listener
this.visible = true;
xspeed=yspeed=0;
removeEventListener(Event.ENTER_FRAME, moveBall);
// sets balls height to above the center of the stage
y=- height;
x=stage.stageWidth/2;
//resets ball's depth perception
var scale : Number = (0.5 * 0.6) + 0.6;
// drops ball back onto the stage to start new round
new TweenLite(this,1.5,{y:stage.stageHeight/2,scaleX:scale,scaleY:scale,ease:Bounce.easeOut,onComplete:start});
}
public function start():void {
// starts ball movement
xspeed=-15;
yspeed=12;
addEventListener(Event.ENTER_FRAME, moveBall);
}
private function moveBall(_e:Event):void {
// call to depth and collision functions
depth();
collision();
// makes ball move
x+=xspeed;
y+=yspeed;
}
private function depth():void {
// allows ball to change size to give depth perseption to tha ball
var smaller : Number = ((y/stage.stageHeight) * 0.6) + 0.6;
scaleX=scaleY=smaller;
}
private function collision():void {
// sets maximum wall values
if (y>=380||y<=155) {
// reverses ball's direction if collision detected
yspeed*=-1;
}// end if
// sets hit test to detect if ball hits player or AI paddles
if (x > (stage.stageWidth / 2) && hitTestObject(MovieClip(parent).bg.wallRight)) {
// changes direction of ball if collision detected
xspeed*=-1;
if (xspeed>0) {
xspeed=- xspeed;
}
}
if (x < (stage.stageWidth / 2) && hitTestObject( MovieClip(parent).bg.wallLeft) ) {
xspeed*=-1;
if (xspeed<0) {
xspeed=- xspeed;
}
}
// checks to see if ball is still located on floor
if (!MovieClip(parent).bg.floor.hitTestPoint(x, y + (height / 2 * scaleY), true)) {
// if not on stage detect which side ball exits on and add score to appropriate side
if (x<stage.stageWidth/2) {
scoreRight++;
MovieClip(parent).bg.wallTop.scoreRight.text = scoreRight;
} else {
scoreLeft++;
MovieClip(parent).bg.wallTop.scoreLeft.text = scoreLeft;
}
if ((scoreLeft >= maxScore) || (scoreRight >= maxScore)) {
fScore();
} else {
drop();
}
}
}
public function fScore():void {
//trace(MovieClip(parent).fScore.winOrLose);
//trace(MovieClip(parent).fScore.winOrLose.textField)
MovieClip(parent).bg.wallRight.alpha=0;
MovieClip(parent).bg.wallLeft.alpha=0;
MovieClip(parent).bg.wallTop.alpha=0;
MovieClip(parent).bg.wallBottom.alpha=0;
MovieClip(parent).bg.floor.lines.alpha=0;
MovieClip(parent).bg.rotationX=0;
this.visible = false;
Line 110 MovieClip(parent).startText.addEventListener (MouseEvent.MOUSE_UP, gameStart );// THIS IS THE LINE OF CODE THAT IS GIVING ME THE ERROR
MovieClip(parent).startText.visible = true;
MovieClip(parent).startText.buttonMode = true;
if (scoreRight >= maxScore) {
MovieClip(parent).fScore.visible = true;
MovieClip(parent).fScore.winOrLose.text = "You Win! ";
//trace ("you win");
}
if (scoreLeft >= maxScore) {
MovieClip(parent).fScore.visible = true;
MovieClip(parent).fScore.winOrLose.text = "You Lose! ";
//trace ("you lose");
}
}
}// end class
}// end package
Here is the error I am getting in the Compiler:
Location: Ball.as, Line 110
Description: 1120: Access of undefined property gameStart.
Source: MovieClip(parent).startText.addEventListener (MouseEvent.MOUSE_UP, gameStart );
Can someone please point me in the right direction?