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

时间:2011-12-08 06:11:27

标签: actionscript-3 oop dice

我正在创建一个棋盘游戏,我正在使用面向对象的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;
    }}}

Gameboard类:

package  {
import flash.display.MovieClip;
public class gameboard extends MovieClip {
    public function gameboard() {
        trace("Gameboard Created");
        stop();}}}

DiceButton类:

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。

此外,如果有人知道如何触发标记的帧,取决于圆圈落在哪个方格上将是一个加号。

2 个答案:

答案 0 :(得分:0)

首先,你的框架是什么意思?一个实际的动画帧,或一个正方形的框架? 如果我正确理解了您的代码,您可以创建Array Pointprotected 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; } 个对象,并将这些对象全部对应于主板上的位置。然后每次只将die值添加到索引中。

主板类:

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
}

gameBoard class:

{{1}}

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

答案 1 :(得分:0)

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

//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...
     }
}