我正在创建一个棋盘游戏,我使用面向对象编程与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);
}}}
如果有人能给一些见解,将是非常有帮助的。gameboard的mc实例是gameboard .
另外,如果有人知道如何根据圆圈落在哪个正方形上来触发标记帧,那将是一个加分项。
如果你希望一个动作的发生取决于另一个对象中另一个动作的结果,你可以首先使用事件调度在对象之间进行通信,并将相关值从一个对象发送到另一个对象。
//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...
}
}
首先,你所说的框架是什么意思?一个真正的动画帧,还是一个围绕正方形的帧?如果我正确理解你的代码,你可以创建Point
对象的Array
,并使它们都对应于棋盘上的位置。然后每次只需将模具值添加到索引中。
主板类:
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
}
如果这对你没有帮助,我很抱歉,但你的问题有点不清楚。我建议你试着给出更多的描述,例如,你所说的框架是什么意思?你是否使用动画来移动玩家?