2

我正在创建一个棋盘游戏,并且正在使用 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();}}}

骰子按钮类:

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);
    }}}

如果有人可以请提供一些非常有帮助的见解。游戏板的 mc 实例是 gameBoard。

此外,如果有人知道如何根据圆圈落在哪个正方形上来触发标记框架,那将是一个加号。

4

2 回答 2

0

首先,框架是什么意思?一个实际的动画帧,还是一个正方形的帧?如果我正确理解您的代码,您可以创建一个Array对象Point并使这些对象都对应于板上的位置。然后每次只需将骰子值添加到索引中。

主板类:

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
}

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

于 2011-12-08T06:40:18.083 回答
0

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

//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...
     }
}
于 2011-12-08T07:15:17.280 回答