Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3-OOP。如何根据骰子的值使对象在游戏板上移动?_Actionscript 3_Oop_Dice - Fatal编程技术网

Actionscript 3 AS3-OOP。如何根据骰子的值使对象在游戏板上移动?

Actionscript 3 AS3-OOP。如何根据骰子的值使对象在游戏板上移动?,actionscript-3,oop,dice,Actionscript 3,Oop,Dice,我正在创建一个棋盘游戏,我正在使用AS3的面向对象编程。我制作了一个电影剪辑,其中有一个圆圈在游戏板上移动。有18个正方形和18个框架。我有一个按钮,它通过随机数功能给你一个骰子值: public function rollDie():void {_dieValue = Math.ceil(Math.random()*6) this.gotoAndStop(_dieValue);} 我有一个骰子按钮、骰子、游戏板和主板的类。我试图使圆圈在棋盘上移动(或转到mc中的帧),

我正在创建一个棋盘游戏,我正在使用AS3的面向对象编程。我制作了一个电影剪辑,其中有一个圆圈在游戏板上移动。有18个正方形和18个框架。我有一个按钮,它通过随机数功能给你一个骰子值:

public function rollDie():void
    {_dieValue = Math.ceil(Math.random()*6)
        this.gotoAndStop(_dieValue);}
我有一个骰子按钮、骰子、游戏板和主板的类。我试图使圆圈在棋盘上移动(或转到mc中的帧),这取决于我用骰子得到的值。以下是我目前的代码:

主板:

package 
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;

public class DiceOut extends MovieClip
{
           public function DiceOut()
       {
                   trace("class diceout defined");
        createListeners();
    }

    public function createListeners():void
    {
        //trace("createListeners");
        rollButton.addEventListener(MouseEvent.CLICK, buttonClick);
    }

    public function buttonClick(e:MouseEvent):void
    {
        die1.rollDie();
        trace(die1.dieValue);
    }}}
骰子等级:

package  {

import flash.display.MovieClip;

public class die extends MovieClip {

    private var _dieValue:uint;

    public function die() {
        trace("dice created");
        stop();
    }
    public function rollDie():void
    {
        _dieValue = Math.ceil(Math.random()*6)
        this.gotoAndStop(_dieValue);
    }
    public function get dieValue():uint
    {
        return _dieValue;
    }}}
游戏板类:

package  {
import flash.display.MovieClip;
public class gameboard extends MovieClip {
    public function gameboard() {
        trace("Gameboard Created");
        stop();}}}
protected var playerIndex:Number = 0;

public function buttonClick(e:MouseEvent):void
{
    die1.rollDie();

    playerIndex += die1.dieValue;// This will move the player forward the number on the die
}

// add this to the createListeners method:
    playerCircle.addEventListener(Event.ENTER_FRAME, PlayerEnt);

public function PlayerEnt (e:Event) {
    // this retrieves the position of the square that the player is on
    playerCircle.x = gameBoard.boardPositions[playerIndex].x;
    playerCircle.y = gameBoard.boardPositions[playerIndex].y;
}
private var boardPositions:Array;

public function gameboard() {
    trace("Gameboard Created");
    stop();

    boardPositions = new Array(//Insert positions here, there are lots of ways of making this information so I won't go into them
}
按钮类:

package  {  
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class GameButton extends MovieClip {
    public function GameButton() {
        trace("Button created");
        stop(); 
        createListeners();
    }
    private function createListeners():void
    {
        this.addEventListener(MouseEvent.MOUSE_OVER, hoverOver);
        this.addEventListener(MouseEvent.MOUSE_OUT, hoverOff);
    }
    public function hoverOver(e:MouseEvent):void
    {
        this.gotoAndStop(2);
    }
    public function hoverOff(e:MouseEvent):void
    {
        this.gotoAndStop(1);
    }}}
如果有人能提供一些见解,那将非常有帮助。gameboard的mc实例是gameboard


另外,如果有人知道如何根据圆圈落在哪个正方形上触发带标签的帧,那将是一个加号。

首先,你所说的帧是什么意思?一个真实的动画帧,还是一个围绕正方形的帧? 如果我正确理解了您的代码,您可以创建
对象的
数组
,并使这些对象都对应于电路板上的位置。然后每次只需将die值添加到索引中

主板类别:

package  {
import flash.display.MovieClip;
public class gameboard extends MovieClip {
    public function gameboard() {
        trace("Gameboard Created");
        stop();}}}
protected var playerIndex:Number = 0;

public function buttonClick(e:MouseEvent):void
{
    die1.rollDie();

    playerIndex += die1.dieValue;// This will move the player forward the number on the die
}

// add this to the createListeners method:
    playerCircle.addEventListener(Event.ENTER_FRAME, PlayerEnt);

public function PlayerEnt (e:Event) {
    // this retrieves the position of the square that the player is on
    playerCircle.x = gameBoard.boardPositions[playerIndex].x;
    playerCircle.y = gameBoard.boardPositions[playerIndex].y;
}
private var boardPositions:Array;

public function gameboard() {
    trace("Gameboard Created");
    stop();

    boardPositions = new Array(//Insert positions here, there are lots of ways of making this information so I won't go into them
}
游戏板类:

package  {
import flash.display.MovieClip;
public class gameboard extends MovieClip {
    public function gameboard() {
        trace("Gameboard Created");
        stop();}}}
protected var playerIndex:Number = 0;

public function buttonClick(e:MouseEvent):void
{
    die1.rollDie();

    playerIndex += die1.dieValue;// This will move the player forward the number on the die
}

// add this to the createListeners method:
    playerCircle.addEventListener(Event.ENTER_FRAME, PlayerEnt);

public function PlayerEnt (e:Event) {
    // this retrieves the position of the square that the player is on
    playerCircle.x = gameBoard.boardPositions[playerIndex].x;
    playerCircle.y = gameBoard.boardPositions[playerIndex].y;
}
private var boardPositions:Array;

public function gameboard() {
    trace("Gameboard Created");
    stop();

    boardPositions = new Array(//Insert positions here, there are lots of ways of making this information so I won't go into them
}

如果这对你没有帮助,我很抱歉,但是你的问题有点不清楚。我建议你试着给出更多的描述,例如,你说的框架是什么意思?您是否使用动画移动播放器?

首先,您所说的帧是什么意思?一个真实的动画帧,还是一个围绕正方形的帧? 如果我正确理解了您的代码,您可以创建
对象的
数组
,并使这些对象都对应于电路板上的位置。然后每次只需将die值添加到索引中

主板类别:

package  {
import flash.display.MovieClip;
public class gameboard extends MovieClip {
    public function gameboard() {
        trace("Gameboard Created");
        stop();}}}
protected var playerIndex:Number = 0;

public function buttonClick(e:MouseEvent):void
{
    die1.rollDie();

    playerIndex += die1.dieValue;// This will move the player forward the number on the die
}

// add this to the createListeners method:
    playerCircle.addEventListener(Event.ENTER_FRAME, PlayerEnt);

public function PlayerEnt (e:Event) {
    // this retrieves the position of the square that the player is on
    playerCircle.x = gameBoard.boardPositions[playerIndex].x;
    playerCircle.y = gameBoard.boardPositions[playerIndex].y;
}
private var boardPositions:Array;

public function gameboard() {
    trace("Gameboard Created");
    stop();

    boardPositions = new Array(//Insert positions here, there are lots of ways of making this information so I won't go into them
}
游戏板类:

package  {
import flash.display.MovieClip;
public class gameboard extends MovieClip {
    public function gameboard() {
        trace("Gameboard Created");
        stop();}}}
protected var playerIndex:Number = 0;

public function buttonClick(e:MouseEvent):void
{
    die1.rollDie();

    playerIndex += die1.dieValue;// This will move the player forward the number on the die
}

// add this to the createListeners method:
    playerCircle.addEventListener(Event.ENTER_FRAME, PlayerEnt);

public function PlayerEnt (e:Event) {
    // this retrieves the position of the square that the player is on
    playerCircle.x = gameBoard.boardPositions[playerIndex].x;
    playerCircle.y = gameBoard.boardPositions[playerIndex].y;
}
private var boardPositions:Array;

public function gameboard() {
    trace("Gameboard Created");
    stop();

    boardPositions = new Array(//Insert positions here, there are lots of ways of making this information so I won't go into them
}

如果这对你没有帮助,我很抱歉,但是你的问题有点不清楚。我建议你试着给出更多的描述,例如,你说的框架是什么意思?是否使用动画移动播放机?

如果希望根据另一个对象中另一个操作的结果来执行某个操作,可以首先使用事件调度在对象之间进行通信,并将相关值从一个对象发送到另一个对象

//In the Die class
public function rollDie():void
{
    _dieValue = Math.ceil(Math.random()*6)
    this.gotoAndStop(_dieValue);

    //assuming that all your Objects have the same parent, 
    //namely the main stage
    //also, you would have to create a CustomEvent class...
    parent.dispatchEvent( new CustomEvent(_dieValue ) );
}

//In your Circle class you must listen to the CustomEvent
//you can do that after the Circle has been added to the stage
//check the Event.ADDED_TO_STAGE
private function addedToStage( event:Event ):void
{
     //remove the event listener
     //listen to your CustomEvent
     parent.addEventListener( CustomEvent.DICE_ROLLED , diceRolled );
}

private function diceRolled( event: CustomEvent ):void
{
     var dieValue:int = event.dieValue;

     // now that you have the value in the Circle class
     // you can react accordingly

     //here I use a switch statement but there are other
     //options of course...
     switch( dieValue )
     {
             case 1:
                //go to this square...
                break;

             case 2:
                //go to this other square...
                break;

             etc...
     }
}

如果希望根据另一个对象中另一个操作的结果执行某个操作,可以首先使用事件调度在对象之间进行通信,并将相关值从一个对象发送到另一个对象

//In the Die class
public function rollDie():void
{
    _dieValue = Math.ceil(Math.random()*6)
    this.gotoAndStop(_dieValue);

    //assuming that all your Objects have the same parent, 
    //namely the main stage
    //also, you would have to create a CustomEvent class...
    parent.dispatchEvent( new CustomEvent(_dieValue ) );
}

//In your Circle class you must listen to the CustomEvent
//you can do that after the Circle has been added to the stage
//check the Event.ADDED_TO_STAGE
private function addedToStage( event:Event ):void
{
     //remove the event listener
     //listen to your CustomEvent
     parent.addEventListener( CustomEvent.DICE_ROLLED , diceRolled );
}

private function diceRolled( event: CustomEvent ):void
{
     var dieValue:int = event.dieValue;

     // now that you have the value in the Circle class
     // you can react accordingly

     //here I use a switch statement but there are other
     //options of course...
     switch( dieValue )
     {
             case 1:
                //go to this square...
                break;

             case 2:
                //go to this other square...
                break;

             etc...
     }
}