Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

restart function not working

New Here ,
Jan 30, 2015 Jan 30, 2015

Here is my game code titled, "Connect4.as"

/////////////////////////////////////////////////////////////////////

package

{

  import flash.display.Sprite

  import flash.events.MouseEvent;

  import flash.events.Event;

  import flash.geom.Point;

  import caurina.transitions.Tweener;

  import flash.display.MovieClip;

  import flash.events.TimerEvent;

  import flash.utils.Timer;

  import flash.system.fscommand;

  public class Connect4 extends Sprite

  {

  private var columns:uint;

  private var rows:uint;

  private var board:Array = new Array();

  private var columnWidth:uint;

  private var rowHeight:uint;

  private var currentPlayer:uint = 1;

  private var currentChip;

  //fullscreen mode

  public function FSCommand(){

  fscommand("fullscreen", "true");

  }

  public function Connect4(columns:uint,rows:uint):void

  {

  this.columns = columns

  this.rows = rows

  columnWidth = new BoardPiece().width

  rowHeight = new BoardPiece().height

  drawboard();

  createboardArray();

  putChipReady();

  this.addEventListener(MouseEvent.CLICK, boardClick)

  this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

  }

  private function drawboard():void

  {

  for(var i:uint = 0; i<rows; i++)

  {

  for(var j:uint = 0; j<columns; j++)

  {

  var boardpiece:BoardPiece = new BoardPiece();

  boardpiece.x = j * boardpiece.width;

  boardpiece.y = i * boardpiece.height;

  this.addChild(boardpiece);

  }

  }

  }

  private function createboardArray():void

  {

  for(var i:uint = 0; i<rows; i++)

  {

  board = []

  for(var j:uint=0; j<columns; j++)

  {

  board = 0;

  }

  }

  }

  private function putChipReady():void

  {

  if(currentPlayer == 1)

  {

  currentChip = new RedChip();

  }

  else

  {

  currentChip = new YellowChip();

  }

  currentChip.y = -50;

  this.addChildAt(currentChip,0);

  }

  private function boardClick(e:MouseEvent):void

  {

  var columnclicked:uint = calculateColumn(e.currentTarget.mouseX);

  for(var row:int=rows-1; row>=0; row--)

  {

  if(board[row][columnclicked]==0)

  {

  board[row][columnclicked] = currentPlayer;

  placeChip(new Point(row,columnclicked))

  if(checkForWinner(new Point(row,columnclicked)))

  {

  trace("WINNAAR");

  this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);

  this.removeEventListener(MouseEvent.CLICK, boardClick);

  showWinnerDialog();

  //add go to next frame here

  }

  else

  {

  togglePlayer();

  putChipReady();

  }

  return

  }

  }

  }

  private function placeChip(position:Point):void

  {

  var distanceY:int = position.x * rowHeight + rowHeight/2;

  var distanceX:int = position.y * columnWidth + columnWidth/2;

  Tweener.addTween(currentChip, {x: distanceX, y:distanceY, time:0.7, transition:"easeOutBounce"});

  }

  private function enterFrameHandler(e:Event):void

  {

  var currentcolumn:uint = calculateColumn(this.mouseX);

  var xPosChip:uint = currentcolumn * columnWidth + columnWidth/2

  Tweener.addTween(currentChip, {x:xPosChip, time:0.3, transition:"lineair"});

  }

  private function checkForWinner(position:Point):Boolean

  {

  if(verticalCheck(position))return true;

    if(horizontalCheck(position))return true;

    if(leftUpDiagonalCheck(position))return true;

    if(rightUpDiagonalCheck(position))return true;

    return false;

  }

  function verticalCheck(position:Point):Boolean

  {

  var row:uint = position.x;

  var column:uint = position.y;

  var player:uint = board[row][column];

  if (row >= rows - 3)

  {

  return false;

  }

  for (var i:uint = row+1; i <= row + 3; i++)

  {

  if (board[column] != player)

  {

  return false;

  }

  }

  return true;

  }

  function horizontalCheck(position:Point):Boolean

  {

  var row:uint = position.x;

  var column:uint = position.y;

  var player:uint = board[row][column];

  var counter:uint = 1;

  for(var i:uint = column-1; i>=0; i--)

  {

  if(board[row] != player)

  {

  break;

  }

  counter++;

  }

  for(var j:uint = column+1; j<columns; j++)

  {

  if(board[row] != player)

  {

  break;

  }

  counter++;

  }

  if(counter >=4)

  {

  return true;

  }

  else

  {

  return false;

  }

  }

  function leftUpDiagonalCheck(position:Point):Boolean

  {

  var player:uint = board[position.x][position.y];

  var row:Number = position.x - 1;

  var column:Number = position.y - 1;

  var counter:uint = 1;

  while (row >= 0 && column >= 0)

  {

  if (board[row][column] == player)

  {

  counter++;

  row--;

  column--;

  }

  else

  {

  break;           

  }

  }

  row = position.x + 1;

  column = position.y + 1;

  while (row < rows && column < columns)

  {           

  if (board[row][column] == player)

  {

  counter++;

  row++;

  column++;

  }

  else

  {

  break;

  }

  }

  if(counter >=4)

  {

  return true;

  }

  else

  {

  return false;

  }

  }

  private function rightUpDiagonalCheck(position:Point):Boolean

  {

  var player:uint = board[position.x][position.y];

  var row:Number = position.x + 1;

  var column:Number = position.y - 1;

  var counter:uint = 1;

  while (row < rows && column >= 0)

  {

  if (board[row][column] == player)

  {

  counter++;

  row++;

  column--;

  }

  else

  {

  break;           

  }

  }

  row = position.x - 1;

  column = position.y + 1;

  while (row >= 0 && column < columns)

  {           

  if (board[row][column] == player)

  {

  counter++;

  row--;

  column++;

  }

  else

  {

  break;

  }

  }

  if(counter >=4)

  {

  return true;

  }

  else

  {

  return false;

  }

  }

  private function showWinnerDialog():void

  {

  var dialog:WinnerDialog = new WinnerDialog();

  var winner:String = (currentPlayer == 1)? "red" : "yellow"

  dialog.txtWinner.text = winner + " wins!!!";

  dialog.x = (this.width - dialog.width)/2;

  dialog.y = 100;

  this.addChild(dialog);

  }

  //helpfunctions

  private function togglePlayer():void

  {

  if(currentPlayer == 1)

  {

  currentPlayer = 2

  }

  else

  {

  currentPlayer = 1

  }

  }

  private function calculateColumn(mouseXPos):uint

  {

  if(mouseXPos < 0)

  {

  return 0;

  }

  else if(mouseXPos > this.width)

  {

  return columns - 1;

  }

  else

  {

  return mouseXPos/columnWidth;

  }

  }

  private function dialog(combi:Array = null):void

  {

  for(var i:uint = 0; i<rows; i++)

  {

  for(var j:uint = 0; j<columns; j++)

  {

  var boardpiece:BoardPiece = new BoardPiece();

  boardpiece.x = j * boardpiece.width;

  boardpiece.y = i * boardpiece.height;

  this.removeChild(boardpiece);

  }

  var timer:Timer = new Timer(5000, 1);

  timer.addEventListener(TimerEvent.TIMER_COMPLETE, restart);

  timer.start();

  }

  }

  private function restart(e:TimerEvent):void

  {

  dispatchEvent(new Event("restart"));

  }

  }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

And here is my main doc class titled, "Application.as"

//

package

{

  import flash.display.MovieClip;

  import flash.events.Event;

  import flash.events.TimerEvent;

  import flash.utils.Timer;

  import flash.events.MouseEvent;

  public class Application extends MovieClip

  {

  public function Application():void

  {

  stage.align = "TL"

  var connect4:Connect4 = new Connect4(7,6);

  connect4.x = (stage.stageWidth - connect4.width)/2;

  connect4.y = (stage.stageHeight - connect4.height)/2 + 50;

  this.addChild(connect4);

  }

  public function restart(e:TimerEvent):void

  {

  stage.align = "TL"

  var connect4:Connect4 = new Connect4(7,6);

  connect4.x = (stage.stageWidth - connect4.width)/2;

  connect4.y = (stage.stageHeight - connect4.height)/2 + 50;

  this.addChild(connect4);

  }

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////

I am (obviously) a novice and having to learn code on the fly for my job. This is a Connect Four game I put together using an online tutorial.
The original game had no restart function, therefore I had to add some code of my own. I'm getting no errors and the actual game works, but isn't restarting.

Fyi, the game is operating from one scene and one frame and there is no code on the main timeline (all in packaged AS files). Any help would be greatly appreciated! Thanks in advance!

TOPICS
ActionScript
325
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2015 Jan 30, 2015

I apologize as I should have triple checked this before posting! 

Please disregard my note "//add go to next frame here" that I left underneath, showWinnerDialog();.

I'm trying to operate strictly on one frame and one scene.Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 30, 2015 Jan 30, 2015

I can see the call to restart at the end of the Connect4 class but it only dispatches an event rather than calling a function.

      dispatchEvent(new Event("restart"));

What I don't see is an event listener for that event in the Application class.  One should be created just after the Connect4 object is created.

     var connect4:Connect4 = new Connect4(7,6);

     connect4.x = (stage.stageWidth - connect4.width)/2;

     connect4.y = (stage.stageHeight - connect4.height)/2 + 50;

     this.addChild(connect4);

     connect4.addEventListener("restart", restart);

You will probably want to remove the current Connect4 object before adding the new one.

One thing you should consider doing with the Application class is to avoid repeating the same code so that you are always sure where what is occurring.  In this case you could just have the Application function calling the restart function... just change the function definition to...

     public function restart(e:TimerEvent=null):void

so that you can call it without the TimerEvent involvement

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2015 Jan 30, 2015

Thank you so much for your response! I was hoping you would be the one to respond since I've seen how much you've helped others.

Let me say again that I am extremely new to this and trying to comprehend and implement correctly.

I added "connect4.addEventListener("restart", restart);" as you suggested.

Although, I tried to remove the Connect4 object before the addition of the new one and it seems to not be working. Do you know what the exact code would be for that? And this is in my Application.as file, correct?

I do want to avoid repetition. So I added the "=null" to the restart function. But, is this in the Application.as file or in the Connect.as file where I need to do this?  Is there anything else I need to change/add to my Connect.as file?


Sorry for the confusion and all the questions!

Thank you SO much for your help!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 31, 2015 Jan 31, 2015
LATEST

The =null goes in the Application file.  What it does is allow you to call that function without having to supply the TimerEvent argument.  So for the Application function you could replace everything in it with just...  restart();

As far as removing the Connect4 object, you should probably have that as a globally defined variable rather than scoped only inside the restart function.  That way it can be referenced as the old to remove it and then reassigned as the new to create it.  Here's the class rewritten (I think it's okay but it's untested)...

package

{

  import flash.display.MovieClip;

  import flash.events.Event;

  import flash.events.TimerEvent;

  import flash.utils.Timer;

  import flash.events.MouseEvent;

  public class Application extends MovieClip

  {

      private var connect4:Connect4;

      public function Application():void

      {

          restart();

      }

      public function restart(e:TimerEvent=null):void

      {

          if(this.connect4 != null){

             this.removeChild(connect4);

             connect4 = null;

          }

          stage.align = "TL";

          connect4 = new Connect4(7,6);

          connect4.x = (stage.stageWidth - connect4.width)/2;

          connect4.y = (stage.stageHeight - connect4.height)/2 + 50;

          this.addChild(connect4);

          connect4.addEventListener("restart", restart);

      }

   }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines