Skip to main content
Participant
September 25, 2015
Question

Actionscript 3 Game Stuck On Loop

  • September 25, 2015
  • 2 replies
  • 545 views

Hello,

I am currently doing a course online and I am suppose to be making an Actionscript 3 Pong game with a splash screen that goes to the game after the player has put their name in and picked a difficulty level, however the game is stuck on a loop and I cant figure out why.

The splash screen is on the 'splash' scene and the game is in the 'gamePlay' scene. Below is the code. Any help would be appreciated.

SPLASH SCREEN SCENE

import flash.media.Sound;

import flash.events.MouseEvent;

import flash.net.URLRequest;

import flash.media.SoundChannel;

stop();

var playerName:String = "";

MovieClip(root).playerName;

//Use this code when assigning the value to to player name

MovieClip(root).playerName = "";

var difficultyLevel:Number = 0; //Lower number for hard, Higher for easier

var noNameSound:Sound = new NoName();

function checkName():void

{

    playerName = firstName_txt.text;

   

    //Check to see if a name string has been entered, if so proceed to game

    if (playerName != "") {

        gotoAndStop ("gamePlay");

    }

    //If no name string is entered, then prompt the player to type one in

    else {

        gotoAndStop ("splash");

        noNameSound.play();

        feedback_txt.text = "Please Enter Your Name";

    }

}

//Create EventListeners for difficulty buttons

easy_btn.addEventListener(MouseEvent.CLICK, easyLevel);

medium_btn.addEventListener(MouseEvent.CLICK, mediumLevel);

hard_btn.addEventListener(MouseEvent.CLICK, hardLevel);

//Create fucntions for difficulty buttons

function easyLevel(event:MouseEvent):void

{

    difficultyLevel = 6;

    checkName();

}

function mediumLevel (event:MouseEvent):void

{

    difficultyLevel = 3;

    checkName();

}

function hardLevel (event:MouseEvent):void

{

    difficultyLevel = 1;

    checkName();

}

function rand(min:int, max:int):int

{

    return Math.random() * (max - min + 1) + min;

}

//Setup splash music random selection

var musicSelect:int = rand(1,4);

var splash_sound:Sound = new Sound();

splash_sound.load(new URLRequest("splash_loop_"+ musicSelect + ".mp3"));

var splash_channel:SoundChannel = new SoundChannel();

splash_channel = splash_sound.play(0,99);

//Setup user mouse or keyboard selection

var userMode:Boolean;

userMode=true;

//Select mouse by default

keyboardTick.visible = false;

mouseTick.visible = true;

keyboard_but.addEventListener(MouseEvent.CLICK, setKeyboard);

mouse_but.addEventListener(MouseEvent.CLICK, setMouse);

function setKeyboard(event:MouseEvent):void

{

    keyboardTick.visible = true;

    mouseTick.visible = false;

    userMode = false;

}

function setMouse(event:MouseEvent):void

{

    keyboardTick.visible = false;

    mouseTick.visible = true;

    userMode = true;

}

GAMEPLAY SCREEN SCENE

import flash.media.Sound;

import flash.display.MovieClip;

import flash.events.MouseEvent;

import flash.events.Event;

import flash.ui.Keyboard;

import flash.events.KeyboardEvent;

var global:MovieClip = MovieClip(root);

trace(global.playerName);

//Use this code to access the value

global.playerName;

// Set variables for ball movement

var xDirection:Number = 10;

var yDirection: Number = -1;

// Place the ball on the stage with an initial random y value

ball_mc.y = Math.random() * 455;

// Set fluid motion ease for player paddle

var targetY:Number = paddle_mc.y;

var easing:Number = 6;

// Set game play element variables

var playerScore:Number;

var enemyScore:Number;

var server:Number = 0;

//var difficultyLevel:Number = 4; // Lower number for hard, Higher for easier

//trace(difficultyLevel);

var winningScore:Number = 3; // This variable controls the winning score to reach

playerName_txt.text = playerName;

enemyName_txt.text = "COMPUTER";

// Set audio variables

var playPaddleSound:Sound = new PaddleHit();

var playBoundarySound:Sound = new BoundaryHit();

var playGameSound: Sound = new PlayGame();

var playerScoreSound:Sound = new PlayerScore();

var enemyScoreSound:Sound = new EnemyScore();

var gameOverSound:Sound = new GameOver();

// This function will reset the position of the ball when a player has scored.

function resetBallPosition():void

{

    if (server == 0)

    {

        xDirection = 10;

    }

    else if (server ==1)

    {

        xDirection = -10;

    }

   

    yDirection = -1;

    ball_mc.y = Math.random() * 455;

    ball_mc.x = 316;

   

}

/*This function will check where the ball hits the paddle, and calculate how fast and in which direction the ball will come back towards the other player*/

function checkHitLocation(paddle_mc: MovieClip):void {

    var hitPercent:Number;

    var ballPosition:Number = ball_mc.y - paddle_mc.y;

    hitPercent = (ballPosition / (paddle_mc.height - ball_mc.height)) - .5;

    yDirection = hitPercent * 20;

    if (difficultyLevel == 1 && playerScore >= 5)  {

        xDirection *= 2.05;

        // This numberf can be changed to in/decrease the ball speed

    } else {

    xDirection *= 1.05;

        //This number can be changed to in - or decrease the speed of the ball

}

}

// This function will show who wins the game, and how many points each player has.

function showScore():void

{

    if(playerScore >= winningScore)

    {

        player_txt.text = "Winner";

        enemy_txt.text = "Loser";

        endGame();

    }

    else if (enemyScore >= winningScore)

    {

        enemy_txt.text = "Winner";

        player_txt.text = "Loser";

        endGame();

    }

    else

    {

        player_txt.text = "" + playerScore;

        enemy_txt.text = "" + enemyScore;

    }

}

//This function will run at end of the game, and reset the eventlisteners

function endGame():void

{

    paddle_mc.removeEventListener(Event.ENTER_FRAME, movePaddle);

    enemy_mc.removeEventListener(Event.ENTER_FRAME, moveEnemy);

    ball_mc.removeEventListener(Event.ENTER_FRAME, moveBall);

    play_but.addEventListener(MouseEvent.CLICK, initializeGame);

   

    server = 0;

    Mouse.show();

    play_but.alpha = 100;

    gameOverSound.play();

   

}

//This function will run automatically when the game starts

function initializeGame(event:MouseEvent):void {

    playGameSound.play();

    playerScore = 0;

    enemyScore = 0;

   

    player_txt.text = "" + playerScore;

    enemy_txt.text = "" + enemyScore;

    if (userMode == true) {

    paddle_mc.addEventListener(Event.ENTER_FRAME, movePaddle);

    enemy_mc.addEventListener(Event.ENTER_FRAME, moveEnemy);

    ball_mc.addEventListener(Event.ENTER_FRAME, moveBall);

    play_but.removeEventListener(MouseEvent.CLICK, initializeGame);

   

    Mouse.hide();

    play_but.alpha = 0;

} else {

   

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);

    stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);

    stage.addEventListener(Event.ENTER_FRAME, moveFunction);

    enemy_mc.addEventListener(Event.ENTER_FRAME, moveEnemy);

    ball_mc.addEventListener(Event.ENTER_FRAME, moveBall);

    play_but.removeEventListener(MouseEvent.CLICK, initializeGame);

   

    Mouse.hide();

    play_but.alpha = 0;

   

}

}

//Keyboard code

var upPress: Boolean;

var downPress: Boolean;

var paddleSpeed: Number = 12;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);

stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);

stage.addEventListener(Event.ENTER_FRAME, moveFunction);

function keyDownFunction (event: KeyboardEvent) {

    switch (event.keyCode) {

        case 38:

            downPress = true;

        break;

       

        case 40:

            upPress = true;

        break;

    }

}

function keyUpFunction(event: KeyboardEvent) {

    switch (event.keyCode) {

       

        case 38:

            downPress = false;

        break;

       

        case 40:

            upPress = false;

        break;

    }

}

function moveFunction(event: Event) {

    if (upPress) {

        paddle_mc.y += paddleSpeed;

    }

    if (downPress) {

        paddle_mc.y -= paddleSpeed;

    }

   

    //Managing the walls

   

    if (paddle_mc.y < -20) {

        paddle_mc.y = -20;

    }

    if (paddle_mc.y > 420) {

        paddle_mc.y = 420;

    }

}

// This function moves the ball, and changes the direction when the ball hits the edge

function moveBall(event:Event):void

{

    if (ball_mc.y <=0)

    {

        ball_mc.y = 1;

        yDirection *= -1;

        playBoundarySound.play();

    }

    else if (ball_mc.y >= stage.stageHeight - ball_mc.height)

    {

        ball_mc.y = stage.stageHeight - ball_mc.height - 1

        yDirection *= -1;

        playBoundarySound.play();

    }

    if (ball_mc.hitTestObject(paddle_mc))

    {

        xDirection*= -1;

        playPaddleSound.play();

        checkHitLocation(paddle_mc);

    }

    if (ball_mc.hitTestObject(enemy_mc))

    {

        xDirection *= -1;

        playPaddleSound.play();

        checkHitLocation(enemy_mc);

    }

    if (ball_mc.x <= 0)

    {

        enemyScore++;

        server = 1;

        showScore();

        resetBallPosition();

    }

    else if (ball_mc.x >= stage.stageWidth - ball_mc.width)

    {

        playerScore++;

        server = 0;

        showScore();

        resetBallPosition();

    }

    ball_mc.y += yDirection;

    ball_mc.x += xDirection;

}

//This function makes the player paddle move

function movePaddle(event:Event):void

{

    if (this.mouseY <= paddle_mc.height / 2)

    {

        targetY = 0;

    }

    else if (this.mouseY >= stage.stageHeight - paddle_mc.height / 2)

    {

        targetY = stage.stageHeight - paddle_mc.height;

    }

    else

    {

        targetY = this.mouseY - paddle_mc.height / 2;

    }

    paddle_mc.y += (targetY - paddle_mc.y) / easing;

}

//This function creates the intelligence of the enemy (computer) player

function moveEnemy(event:Event):void

{

    var enemyTargetY:Number;

    enemyTargetY = ball_mc.y - enemy_mc.height / 2;

   

    if (enemyTargetY <= 0)

    {

        enemyTargetY = 0;

    }

    else if (enemyTargetY >= stage.stageHeight - enemy_mc.height)

    {

        enemyTargetY = stage.stageHeight - enemy_mc.height;

    }

    enemy_mc.y += (enemyTargetY - enemy_mc.y) / difficultyLevel;

}

//click on play button to start game

play_but.addEventListener(MouseEvent.CLICK, initializeGame);

This topic has been closed for replies.

2 replies

Inspiring
September 25, 2015

Scenes are problematic - best to never use them.

Put in some trace statements to see what's going on. Specifically in checkName() first

Not sure what this is supposed to do:

var playerName:String = "";

MovieClip(root).playerName;     <-- This does nothing...

//Use this code when assigning the value to to player name

MovieClip(root).playerName = "";

Why do you set a normal var playerName and then do the MovieClip(root) thing? That isn't necessary.

Ned Murphy
Legend
September 25, 2015

If the game is stuck in a loop such that it never stops at the first scene then you likely have a coding error and should be checking the output/compiler panels for errors.  If the looping happens in some other way then you should explain when it starts and what it does.

The gotoAndStop command should be specified as :  gotoAndStop(frame, scene)

if you are using scenes you should probably not, they are a hazard to timeline-based navigation.  Just use separate frames.  THat;s what the design ends up being anyways - all scenes are combined into one timeline during compilation.

If the code is in the main timeline then you should not need to be using any MovieClip(root) targeting.

var playerName:String = "";

MovieClip(root).playerName; // line does nothing

//Use this code when assigning the value to to player name

MovieClip(root).playerName = "";   // assigns an empty string to the name

Above all else, learn to make use of the trace() function to troubleshoot your code.