我目前正和一位朋友一起玩游戏,我需要一些代码的帮助...在我的游戏中我想让球出现在屏幕上,但是按照特定的顺序,所以我创建了一个列表将决定在哪里产生球,以及什么颜色。示例:
var a:Array = [["green", "red", "blue", "purple"],
["orange", "red", "yellow", "black"],
["green", "green", "blue", "pink"]]
inter维数组是因为我希望它们产生的顺序(从底部开始)。我已经想出了如何产生所有球,但是当它们产卵时它们会产生在彼此之上。 有人可以帮我这个吗?谢谢! (我用flash btw制作)
代码我:
import flash.events.Event;
stage.addEventListener(Event.ENTER_FRAME, main);
// Variabels
var row1:Number = 92.25;
var row2:Number = 243.30;
var row3:Number = 394.30;
var row4:Number = 547.35;
var pattern:Array = pattern1;
/*
var array:Array = pattern.reverse();
var rows:Number = pattern1.length;
*/
//trace(pattern.reverse());
//Frame counter
var frame:Number = 0;
function main(e:Event):void {
make(pattern.reverse(), pattern.length);
trace(pattern);
}
function make(array:Array, rows):void {
//Count frames
frame += 1;
//trace(frame);
if (frame % 30 == 0) {
//Go thrugh the rows
for (var r = 0; r<rows; r+=1) {
//trace(array[r]);
//Go thrugh the lists
for (var i = 0; i <= 3; i+= 1) {
spawnBall(array[r][i], i);
}
}
}
/*
// Go through each row
for (var r = 0; r <= rows; r += 1) {
}
// Go through the list and spawn;
for (var i = 0; i <= 3; i+=1) {
spawnBall(array[i], i);
}
*/
}
// Spawn the ball
function spawnBall(color:String, pos:Number):void {
if (color == "###") {
trace("Not a color");
} else {
switch (color) {
case "lil":
var purple_ball:PurpleBall = new PurpleBall;
addChild(purple_ball);
purple_ball.x = setX(pos);
purple_ball.y = 0;
break;
case "gul":
var yellow_ball:YellowBall = new YellowBall;
addChild(yellow_ball);
yellow_ball.x = setX(pos);
yellow_ball.y = 0;
break;
case "grø":
var green_ball:GreenBall = new GreenBall;
addChild(green_ball);
green_ball.x = setX(pos);
green_ball.y = 0;
break;
case "rød":
var red_ball:RedBall = new RedBall;
addChild(red_ball);
red_ball.x = setX(pos);
red_ball.y = 0;
break;
case "blå":
var blue_ball:BlueBall = new BlueBall;
addChild(blue_ball);
blue_ball.x = setX(pos);
blue_ball.y = 0;
break;
case "ros":
var pink_ball:PinkBall = new PinkBall;
addChild(pink_ball);
pink_ball.x = setX(pos);
pink_ball.y = 0;
break;
case "ora":
var orange_ball:OrangeBall = new OrangeBall;
addChild(orange_ball);
orange_ball.x = setX(pos);
orange_ball.y = 0;
break;
}
this.addEventListener(Event.ENTER_FRAME, moveBall)
}
function setX(pos):Number {
var xpos:Number;
switch (pos) {
case 0:
xpos = row1;
break;
case 1:
xpos = row2;
break;
case 2:
xpos = row3;
break;
case 3:
xpos = row4;
break;
}
return xpos;
}
}
//Move the balls
function moveBall(e:Event) {
e.currentTarget.y += 5;
}
答案 0 :(得分:1)
如果我们先进行一些重构,那么解决这个问题会更容易。例如,setX()
很笨拙。那里有很多可能出错的地方。如果不出意外,它可以大大缩写:
// 0 1 2 3
var xPositions:Array = [92.25, 243.30, 394.30, 547.35];
function setX(pos:Number):Number {
return xPositions[pos];
}
spawnBall
也可以简化。
var ballColors:Object = {
"lil": PurpleBall,
"gul": YellowBall,
"grø": GreenBall,
"rød": RedBall,
"blå": BlueBall,
"ros": PinkBall,
"ora": OrangeBall
};
function spawnBall(color:String, pos:Number):void {
var Ball = ballColors[color];
if (Ball == undefined) {
trace("Not a color: " + color);
return
}
var ball = new Ball();
addChild(ball);
ball.x = setX(pos);
ball.y = 0;
this.addEventListener(Event.ENTER_FRAME, moveBall);
}
//Move the balls
function moveBall(e:Event):void {
e.currentTarget.y += 5;
}
现在spawnBall
更易于管理,我们可以更清楚地看到问题。您将moveBall
事件处理程序附加到舞台上,然后让它移动舞台而不是舞台。由于你想同时移动所有的球,有一个事件处理程序附加到舞台上是有道理的,但你需要将一组生成的球保持在一个数组中,这样你就可以更新它们。
让我们将addEventListener()
调用移到初始化逻辑并添加一个全局数组来存储球列表。
spawnBall
变为:
function spawnBall(color:String, pos:Number):void {
var Ball = ballColors[color];
if (Ball == undefined) {
trace("Not a color: " + color);
return
}
var ball = new Ball();
addChild(ball);
ball.x = setX(pos);
ball.y = 0;
spawnedBalls.push(ball);
}
moveBall
变为:
//Move the balls
function moveBall(e:Event):void {
for each (var ball in spawnedBalls) {
ball.y -= 5;
}
}
声明了这个全局变量:
var spawnedBalls:Array = [];
最后,将其添加到main()
:
this.addEventListener(Event.ENTER_FRAME, moveBall);
这应该有效。