0

这是问题所在。我对 Flash 有点陌生,但有一些知识,所以我正在尝试创建一个小足球游戏。我有球,控制,舞台(足球场)问题是我想指定舞台的一部分,所以如果我的球进入它会播放一个尖叫“进球!!!”的声音文件 每次球进入拱门......舞台是 800 x 600。

关于我将如何实现这一点的任何想法?任何帮助是极大的赞赏。谢谢!

这是我的代码:

 

import flash.display.*; import flash.events.*; var begin = new Begin(); var pelota = new Pelota(); var field = new SoccerField(); //Calling methods game beginGame(); //creating the welcome screen function beginGame(){ begin.x = 0; begin.y = 0; addChild(begin); pelota.x = 400; pelota.y = 425; addChild(pelota); } pelota.addEventListener(MouseEvent.CLICK, startGame); function startGame(event:Event) : void{ field.x = 400; field.y = 300; addChild(field); pelota.x = 400; pelota.y = 300; pelota.scaleX = .2; pelota.scaleY = .2; addChild(pelota); } stage.addEventListener(KeyboardEvent.KEY_DOWN, control); function control (evt:KeyboardEvent) : void { trace(evt.keyCode); if (evt.keyCode == Keyboard.LEFT) { pelota.x=pelota.x-5; trace('Left!'); } if (evt.keyCode == Keyboard.RIGHT) { pelota.x=pelota.x+5; } if (evt.keyCode == Keyboard.UP) { pelota.y=pelota.y-5; } if (evt.keyCode == Keyboard.DOWN) { pelota.y=pelota.y+5; } } <pre><code>
4

1 回答 1

0

Two ways off the top of my head you could do this (I guessing the goal area is roughly a rectangle shape):

1.) Create the ball and goal as a movie clip/display object then simply do a hitTestObject () method call to check if they 'touch' each other. The actual 'trigger' area of the goal can be smaller then the visual area so that the ball only can touch it if it is visually all the way inside the goal.

2.) This is a circle vs. rectangle collision detection. You could solve the problem through pure math calculations. You should be able to find the actual algorithm by searching and isn't too difficult to write, especially if the goal-rectangle is axis aligned (at 0 or 90 degrees to the stage).

The second method will work faster and probably be more accurate, though the first can be a quick and dirty way of just getting it to work first. Both methods can just return a boolean that when true, you would play the sound. For playing the sound, look up the Sound{} class.

于 2012-12-03T03:40:12.327 回答