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

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です。

また、円がどの正方形に着地するかに応じて、ラベル付きフレームをトリガーする方法を誰かが知っている場合は、それがプラスになります。

4

2 に答える 2

0

まず、フレームとはどういう意味ですか?実際のアニメーション フレーム、または正方形の周りのフレーム? 私があなたのコードを正しく理解していればArrayPointオブジェクトの を作成し、これらすべてをボード上の位置に対応させることができます。次に、ダイスの値を毎回インデックスに追加します。

メインボードクラス:

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

別のオブジェクトの別のアクションの結果に応じてアクションを発生させたい場合は、最初にイベントディスパッチを使用してオブジェクト間で通信し、関連する値をあるオブジェクトから別のオブジェクトに送信できます。

//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 に答える