晕,我正在开发一款迷你flash游戏。
游戏的玩法: 玩家控制角色并左右移动以收集钱币。 金钱和炸弹有5点从上到下释放。当角色与金钱碰撞然后增加金钱,否则炸弹年龄增加1.一旦年龄是99,游戏结束,钱就是最后的分数
这是我的代码:
package Class
{
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextField;
import flash.ui.Keyboard;
import flash.display.MovieClip;
public class Game extends MovieClip
{
private var bgi:BGI;
private var character:Character;
private var money:Money;
private var bomb:Bomb;
private var moneysource : MoneySource;
private var moneytext:TextField;
private var agetext:TextField;
private var characterspeed:int;
private var objectspeed:int;
private var age:int;
private var moneyscore:int;
private var moneyinscreen:int;
private var moneyVector:Vector.<MovieClip > = new Vector.<MovieClip >;
private var noofmoney:int;
private var noofbomb:int;
private var bombVector:Vector.<MovieClip > = new Vector.<MovieClip >;
public function Game()
{
bgi = null;
character = null;
money = null;
bomb = null;
moneysource = null;
Initialize();
}
private function Initialize()
{
age = 0;
moneyscore = 0;
noofmoney = 15;
noofbomb = 10;
bgi = new BGI ;
bgi.x = 0;
bgi.y = 0;
character = new Character ;
character.x = 335;
character.y = 400;
bomb = new Bomb ;
bomb.x = 40;
bomb.y = 0;
moneytext = new TextField ;
moneytext.x = 450;
moneytext.y = 0;
moneytext.defaultTextFormat = Config.TxtFormat;
moneytext.text = "Money : 0";
moneytext.width = 200;
agetext = new TextField ;
agetext.x = 700;
agetext.y = 0;
agetext.defaultTextFormat = Config.TxtFormat;
agetext.text = "Age : 0";
agetext.width = 100;
Config.CurrentStage.addChild(bgi);
Config.CurrentStage.addChild(character);
Config.CurrentStage.addChild(agetext);
Config.CurrentStage.addChild(moneytext);
moneysource = new MoneySource;
moneysource.x = 61;
moneysource.y = 50;
Config.CurrentStage.addChild(moneysource);
moneysource = new MoneySource;
moneysource.x = 211;
moneysource.y = 50;
Config.CurrentStage.addChild(moneysource);
moneysource = new MoneySource;
moneysource.x = 371;
moneysource.y = 50;
Config.CurrentStage.addChild(moneysource);
moneysource = new MoneySource;
moneysource.x = 531;
moneysource.y = 50;
Config.CurrentStage.addChild(moneysource);
moneysource = new MoneySource;
moneysource.x = 691;
moneysource.y = 50;
Config.CurrentStage.addChild(moneysource);
for (var i:int = 0; i < noofmoney; i++)
{
money = new Money ;
money.vel = RandomRange(6,7);
money.x = RandomRange(0,750);
money.y = RandomRange(0,100);
Config.CurrentStage.addChild(money);
moneyVector.push(money);
}
for (var j:int = 0; j < noofbomb; j++)
{
bomb = new Bomb ;
bomb.vel = RandomRange(6,7);
bomb.x = RandomRange(0,750);
bomb.y = RandomRange(0,100);
Config.CurrentStage.addChild(bomb);
bombVector.push(bomb);
}*/
Config.CurrentStage.addEventListener(Event.ENTER_FRAME,Update);
Config.CurrentStage.addEventListener(KeyboardEvent.KEY_DOWN,Control);
}
private function Update(evt:Event)
{
for (var i:int = 0; i < moneyVector.length; i++)
{
if (moneyVector[i].hitTestObject(character))
{
Config.CurrentStage.removeChild(moneyVector[i]);
moneyVector.splice(i, 1);
moneyscore += 400;
moneytext.text = "Money : " + moneyscore.toString();
money = new Money ;
//money.vel = RandomRange(6,7);
money.x = RandomRange(0,750);
money.y = RandomRange(0,100);
Config.CurrentStage.addChild(money);
moneyVector.push(money);
}
if (moneyVector[i].y > Config.ScreenHeight)
{
moneyVector[i].x = RandomRange(0,750);
moneyVector[i].y = - moneyVector[i].height;
}
}
for (var j:int = 0; j < bombVector.length; j++)
{
if (bombVector[j].hitTestObject(character))
{
Config.CurrentStage.removeChild(bombVector[j]);
bombVector.splice(j, 1);
age++;
agetext.text = "Age : " + age.toString();
bomb = new Bomb ;
bomb.vel = RandomRange(6,7);
bomb.x = RandomRange(0,750);
bomb.y = RandomRange(0,100);
Config.CurrentStage.addChild(bomb);
bombVector.push(money);
}
if (bombVector[j].y > Config.ScreenHeight)
{
bombVector[j].x = RandomRange(0,750);
bombVector[j].y = - bombVector[j].height;
}
}
}
private function Control(evt:KeyboardEvent)
{
characterspeed = 15;
if (evt.keyCode == Keyboard.LEFT)
{
character.x -= characterspeed;
if (character.x <= 0)
{
character.x = 0;
}
}
if (evt.keyCode == Keyboard.RIGHT)
{
character.x += characterspeed;
if (character.x >= 670)
{
character.x = 670;
}
}
}
private function RandomRange(min:Number,max:Number):Number
{
return Math.random() * max - min + min;
}
}
}
这是我的游戏: 目前,炸弹和钱随机出现X,100,屏幕上有15个钱和10个炸弹。 我想要的是,如何修改它变成金钱和炸弹从特定点出现,之后撞到墙上的钱和炸弹将反弹。 有人可以提供一些代码吗?感谢
我的游戏类似游戏的例子是 http://www.youtube.com/watch?v=zzQ8PP5-TVE
答案 0 :(得分:0)
使用Box2d可以轻松实现对钱和炸弹坠落的真实模拟。
这是一个带有完整源代码的演示的链接,其中球被随机添加到一排钉子上方。这与您链接的视频风格相同。
在该示例中,您可以使用选择随机起始位置的函数替换产生球的代码。这是编写这样一个函数的一种方法:
function chooseSpawnPoint():Point{
var points:Array = new Array();
points[0] = new Point(0,50);
points[1] = new Point(0,100);
points[2] = new Point(0,150);
points[3] = new Point(0,200);
points[4] = new Point(0,250);
return Math.floor(Math.random()*points.length);
}
*您可以通过初始化函数调用之外的“points”数组来提高该函数的性能。如果你这样做,它可能会显示如下:
var points:Array = new Array();
points[0] = new Point(0,50);
points[1] = new Point(0,100);
points[2] = new Point(0,150);
points[3] = new Point(0,200);
points[4] = new Point(0,250);
// Add as many points as you want.
// the function will choose one at random based on
// the number of points you add to the points array.
function chooseSpawnPoint():Point{
return Math.floor(Math.random()*points.length);
}