Actionscript 3 动作脚本3。在不同时间生成对象
我正在创建一个游戏,其中有4个不同的物体从天空坠落,需要通过点击鼠标按钮来摧毁它们。我的问题是所有4个对象同时生成。我需要使它随机产生所有4个,但不是在同一时间和玩家有200点后,我需要使它产生和下降更多的对象。(我有计数点的工作计数器)。 还有一件事,如果您知道如何使对象不在另一个对象上生成。 我的意思是: 坏产卵 以下是我的常量变量:Actionscript 3 动作脚本3。在不同时间生成对象,actionscript-3,flash,object,actionscript,time,Actionscript 3,Flash,Object,Actionscript,Time,我正在创建一个游戏,其中有4个不同的物体从天空坠落,需要通过点击鼠标按钮来摧毁它们。我的问题是所有4个对象同时生成。我需要使它随机产生所有4个,但不是在同一时间和玩家有200点后,我需要使它产生和下降更多的对象。(我有计数点的工作计数器)。 还有一件事,如果您知道如何使对象不在另一个对象上生成。 我的意思是: 坏产卵 以下是我的常量变量: public static const GRAVITY:Number = 3; public static const HIT_TOLERANCE:Numb
public static const GRAVITY:Number = 3;
public static const HIT_TOLERANCE:Number = 50;
//Powerup
public static const APPLE_END_Y:Number = 640;
public static const APPLE_SPAWN_CHANCE:Number = 0.02; //per frame per second
public static const APPLE_START_Y:Number = 110;
public static const APPLE_SPAWN_START_X:Number = 50;
public static const APPLE_SPAWN_END_X:Number = 500;
//Scoring
public static const PLAYER_START_SCORE:Number = 0;
public static const SCORE_PER_APPLE:Number = 10;
以下是我的部分代码:
public function startGame()
{
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
tol = C.HIT_TOLERANCE;
apples = new Array();
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
private function update(evt:Event)
{
//Spawn new apples
if (Math.random() < randomChance)
{
//spawn x coordinates
var newPirmas = new Pirmas();
newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newAntras = new Antras();
newAntras.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newTrecias = new Trecias();
newTrecias.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
//spawn y coordinates
newPirmas.y = C.APPLE_START_Y;
newAntras.y = C.APPLE_START_Y;
newTrecias.y = C.APPLE_START_Y;
newApple.y = C.APPLE_START_Y;
apples.push(newPirmas);
apples.push(newAntras);
apples.push(newTrecias);
apples.push(newApple);
newPirmas.addEventListener(MouseEvent.CLICK, onClick);
newAntras.addEventListener(MouseEvent.CLICK, onClick);
newTrecias.addEventListener(MouseEvent.CLICK, onClick);
newApple.addEventListener(MouseEvent.CLICK, onClick);
mcGameStage.addChildAt(newPirmas,0);
mcGameStage.addChildAt(newAntras,0);
mcGameStage.addChildAt(newTrecias,0);
mcGameStage.addChildAt(newApple,0);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--)
{
apples[i].y += gravity;
if (apples[i].y > C.APPLE_END_Y)
{
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
m_iLives--;
if (!m_iLives)
{
trace("Game Over");
// newApple.removeEventListener(MouseEvent.CLICK, onClick);
break;
}
}
}
txtScore.text = String(score);
}
function onClick(evt:MouseEvent):void{
var apples = evt.target;
apples.visible = false;
apples = tol;
score += C.SCORE_PER_APPLE;
}
}
public function startGame()
{
速度=C.PLAYER\u速度;
重力=C.重力;
得分=C.球员开始得分;
randomChance=C.APPLE\u SPAWN\u CHANCE;
tol=C.命中公差;
苹果=新数组();
mcGameStage.addEventListener(事件。输入帧,更新);
}
私有函数更新(evt:事件)
{
//繁殖新苹果
if(Math.random()=0;i--)
{
苹果[i].y+=重力;
if(apples[i].y>C.APPLE\u END\u y)
{
mcGameStage.removeChild(苹果[i]);
苹果.拼接(i,1);
米尔维夫斯——;
如果(!m_iLives)
{
跟踪(“游戏结束”);
//newApple.removeEventListener(MouseEvent.CLICK,onClick);
打破
}
}
}
txtScore.text=字符串(分数);
}
函数onClick(evt:MouseEvent):void{
var=evt.target;
可见=假;
苹果=醇;
分数+=C.每个苹果的分数;
}
}
公共功能启动名()
{
速度=C.PLAYER\u速度;
重力=C.重力;
得分=C.球员开始得分;
randomChance=C.APPLE\u SPAWN\u CHANCE;
tol=C.命中公差;
苹果=[];
itemsToSpawn=[];
mcGameStage.addEventListener(事件。输入帧,更新);
nextSpawn:Number=0;
}
私有函数更新(evt:事件)
{
//繁殖新苹果
if(Math.random()=0;i--)
{
苹果[i].y+=重力;
if(apples[i].y>C.APPLE\u END\u y)
{
mcGameStage.removeChild(苹果[i]);
苹果.拼接(i,1);
米尔维夫斯——;
如果(!m_iLives)
{
跟踪(“游戏结束”);
//newApple.removeEventListener(MouseEvent.CLICK,onClick);
打破
}
}
}
txtScore.text=字符串(分数);
if(itemsToSpawn.length>0&&getTimeout()>nextSpawn)
{
var item:DisplayObject=itemsToAdd.shift();
苹果。推(项目);
mcGameStage.addChild(项目);
nextSpawn=getTimeout()+Math.random()*4000;
}
}
函数onClick(事件:MouseEvent):void
{
var=event.currentTarget;
可见=假;
苹果=醇;
分数+=C.每个苹果的分数;
如果(分数%100==0)
{
重力+=10;
}
}
在更新代码中,您只需检查一次随机值是否小于rando
public function startGame()
{
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
tol = C.HIT_TOLERANCE;
apples = [];
itemsToSpawn = [];
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
nextSpawn:Number = 0;
}
private function update(evt:Event)
{
//Spawn new apples
if (Math.random() < randomChance)
{
//spawn x coordinates
var newPirmas = new Pirmas();
newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newAntras = new Antras();
newAntras.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newTrecias = new Trecias();
newTrecias.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
//spawn y coordinates
newPirmas.y = C.APPLE_START_Y;
newAntras.y = C.APPLE_START_Y;
newTrecias.y = C.APPLE_START_Y;
newApple.y = C.APPLE_START_Y;
newPirmas.addEventListener(MouseEvent.CLICK, onClick);
newAntras.addEventListener(MouseEvent.CLICK, onClick);
newTrecias.addEventListener(MouseEvent.CLICK, onClick);
newApple.addEventListener(MouseEvent.CLICK, onClick);
//add items to itemsToAdd
itemsToAdd.push(newPirmas, newAntras, newTrecias, newApple);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--)
{
apples[i].y += gravity;
if (apples[i].y > C.APPLE_END_Y)
{
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
m_iLives--;
if (!m_iLives)
{
trace("Game Over");
// newApple.removeEventListener(MouseEvent.CLICK, onClick);
break;
}
}
}
txtScore.text = String(score);
if( itemsToSpawn.length > 0 && getTimeout() > nextSpawn )
{
var item:DisplayObject = itemsToAdd.shift();
apples.push(item);
mcGameStage.addChild(item);
nextSpawn = getTimeout() + Math.random() * 4000;
}
}
function onClick(event:MouseEvent):void
{
var apples = event.currentTarget;
apples.visible = false;
apples = tol;
score += C.SCORE_PER_APPLE;
if(score % 100 == 0)
{
gravity += 10;
}
}
private function update(evt:Event){
if(Math.random() < randomValue){
createApple();
}
private function createApple(){
...
var newApple = appleToSpawn[currentApple]
...
}