我目前正在开发一个游戏,其中有一些不同的MovieClip
对象,这些对象由一个计时器事件数组添加到舞台上。
这些影片片段由MouseEvent.MOUSE_DOWN
处理,因此当用户触摸屏幕上显示的正确对象时,nScore
会增加1分,但我想要做的就是用户获得的随机时间量或随机数量的点我希望其中一个对象不再处于活动状态,因此如果触摸不活动的对象,则用户会丢失。然后在随机时间或分数后,对象再次变化,你必须只触摸那个对象而不是屏幕上显示的前一个对象...
所以我创建了两个鼠标事件监听器,如下所示:
mSquare.addEventListener(MouseEvent.MOUSE_DOWN, SquareNotActivated);
mSquare.addEventListener(MouseEvent.MOUSE_DOWN, SquareIsActivated);
所以在我的SquareIsActivated函数中,我有另一个名为SquareActivate
的函数,它有以下代码:
private function SquareActivate(square:DisplayObject):void
{
nScore ++;
updateHighScore();
updateCurrentScore();
}
在我的SquareNotActivated函数中,我有一个名为squareNotActive
的函数,它具有以下代码:
private function squareNotActive(square:DisplayObject):void
{
nLives --;
}
然后对于另一个名为mPop
的Movie Clip对象,我有相同的设置。
我打算尝试为if
创建多个nScore
语句来处理事件监听器的更改以及我要尝试的显示对象,并按照以下方式执行:
private function checkNScore():void
{
if (nScore >= 2)
{
// then remove a mouse listener for one object and add another
stage.addChild(pop_Icon);
pop_Icon.x = (stage.stageWidth / 2) + 180;
pop_Icon.y = (stage.stageHeight / 2) - 300;
}
if (nScore >= 4)
{
//Remove Square icon
pop_Icon.destroyPopIcon();
//Add new Square icon
stage.addChild(square_Icon);
square_Icon.x = (stage.stageWidth / 2) + 180;
square_Icon.y = (stage.stageHeight / 2) - 300;
}
}
但是我知道必须有一种随机调用它们的方法可能是一个定时器对象,它生成一个用户必须触摸的随机MovieClip
,然后放置所有其他影片剪辑以移除生命而不是多个{{ 1}}语句。任何人都可以把我推向正确的方向吗?
答案 0 :(得分:0)
我认为最好扩展MovieClip
为您的可点击对象创建另一个类,并向其添加type
属性。
在计时器事件中,您只需更改目标类型。
通过这种方式,您可以对所有对象使用相同的功能。
package test
{
import flash.display.MovieClip;
/**
* This is a class that extends MovieClip and have a public type property.
*/
public class MyObject extends MovieClip
{
public var type :int;
public function MyObject( type = 0 )
{
super();
this.type = type;
// for this test i just create a circle with a color associated with the type
this.graphics.beginFill( type*50 );
this.graphics.drawCircle(0,0,20);
this.graphics.endFill();
}
}
}
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
import test.MyObject;
public class Main extends Sprite
{
/** the timer to change the type **/
private var timer :Timer;
/** the current target type **/
private var targetType :int;
/** the objects available **/
private var objects :Vector.<MyObject>;
/** the last time of the enterFrameEvent **/
private var lastTime :int;
/** the last time the target type changed **/
private var lastTypeChanged :int;
/** the last time we created an object **/
private var lastObjectCreated :int;
public function Main()
{
objects = new Vector.<MyObject>();
generateObject();
changeTargetType();
lastTime = getTimer();
addEventListener( Event.ENTER_FRAME, onEnterFrame );
}
/** generate a new object and place it on the stage **/
private function generateObject():void
{
// generate a random type
var type:int = int(Math.random()*10)
// create the object
var o:MyObject = new MyObject( type );
// place the object randomly on the stage
o.x = 50 + (Math.random() * 350);
o.y = 50 + (Math.random() * 350);
// add mouse evtn listener to the object
o.addEventListener( MouseEvent.CLICK, handleClick );
// add the object to the display list
addChild( o );
// add the object to the array
objects.push( o );
// reset the timer
lastObjectCreated = 0;
}
private function changeTargetType():void
{
// get an object randomly on the array to be sure the type is available on screen
var typeId:int = Math.random()*objects.length;
// get the type of the random object
targetType = objects[typeId].type;
// reset the timer
lastTypeChanged = 0;
trace( "new Type::",targetType );
}
/** on enter frame, calculate the passed time and see what you want to do **/
private function onEnterFrame( event:Event ):void
{
// calculate passed time
var passedTime:int = getTimer() - lastTime;
//trace( passedTime, lastTime, lastTypeChanged, lastObjectCreated );
lastTime = getTimer();
// add the time to the timers
lastTypeChanged += passedTime;
lastObjectCreated += passedTime;
// create an object each 5 seconds
if( lastObjectCreated >= 5000 ) generateObject();
// change the target type each 10 seconds
if( lastTypeChanged >= 10000 ) changeTargetType();
}
/** handle the click **/
private function handleClick( event:MouseEvent ):void
{
// get the object clicked
var o:MyObject = event.target as MyObject;
// if it is the goo type
if( o.type == targetType )
{
//update the score
trace( "yeah" );
//--> do what you want
//remove the object from the displayList
removeChild(o);
// remove listeners on the object
o.removeEventListener( MouseEvent.CLICK, handleClick );
// remove the object from the array
var i:int = objects.indexOf(o);
objects.splice(i,1);
}
else
{
// remove life.
trace( "bad" );
}
}
}
}
我希望这可以帮到你:)。