2011-12-08 3 views
2

ボードゲームを作成していて、AS3でオブジェクト指向プログラミングを使用しています。私は、ゲームボードを横切って移動する円でムービークリップを作成しました。 18個の正方形と18個のフレームがあります。私はあなたに乱数関数でサイコロを持つ値を与えるのボタンがあります。AS3 OOP。サイコロの価値に応じてゲームボード上でオブジェクトを動かすにはどうすればよいですか?

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

誰かが非常に役立ついくつかの洞察力を与えることができれば。 gameboardのmcインスタンスはgameBoardです。

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

答えて

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

最初のもの、フレームはどういう意味ですか?実際のアニメーションフレーム、または正方形の周りのフレーム? あなたのコードを正しく理解していれば、の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 
} 

これはあなたを助けていない場合、私は申し訳ありませんが、しかし、あなたの質問は少し不明です。私はあなたが何か説明を与えようとしていることをお勧めします。例えば、あなたはフレームによって何ができますか?あなたはアニメーションを使ってプレーヤーを移動していますか?

関連する問題