我一直在努力嘗試使用as3和flash遊戲。一切看起來都很好,但子彈仍然卡在大炮裏面。當我用我的鼠標拍攝,而不是走出去的位置,它只是停留拿到大炮裏面:AS3炮塔和子彈錯誤
了3 AS3文件,以及一個flash文件:
Ships.as
package{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
public class Ship extends Sprite{
private var speed:int;
private var target:Point;
function Ship(){
speed=2+Math.random()*5;
//trace("Made a Ship!");
//set the target for the ships
target = new Point(Math.random()*500, Math.random()*500);
//target.x = Math.random()*500;
//target.y = Math.random()*500;
//addEventListener(Event.ENTER_FRAME, update);
}
function update(){
//Set the ships to point the target
var dx = target.x - x;
var dy = target.y - y;
var angle = Math.atan2(dy, dx)/Math.PI*180;
rotation = angle;
x=x+Math.cos(rotation/180*Math.PI)*speed;
y=y+Math.sin(rotation/180*Math.PI)*speed;
//New target
var hyp = Math.sqrt((dx*dx)+(dy*dy));
if(hyp < 5){
target.x = Math.random()*500;
target.y = Math.random()*500;
}
}
}
}
Game.as
package{
import flash.display.MovieClip;
import flash.events.Event;
public class Game extends MovieClip{
var ships:Array;
public function Game(){
trace("Made that game!");
addEventListener(Event.ENTER_FRAME, loop);
//set up the ship array
ships = new Array();
}
function loop(e:Event){
if(numChildren<10){
var s = new Ship();
addChild(s);
s.x = Math.random()*stage.stageWidth;
s.y = Math.random()*stage.stageHeight;
s.rotation = Math.random()*360;
//Add the ship to the list of ships
ships.push(s);
}
//Make a for loop to iterate through all the ship
for(var count=0; count<ships.length; count++){
ships[count].update();
//Add a new for loop to go through all the bullets
}
}
}
}
}
}
Turret.as
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Turret extends MovieClip{
//Properties goes here
var shotCooldown:int;
var bullets:Array;
const MAX_COOLDOWN = 10;
public function Turret(){
//Set the Shot Cooldown
shotCooldown = MAX_COOLDOWN;
bullets = new Array();
addEventListener(Event.ENTER_FRAME, update);
addEventListener(Event.ADDED_TO_STAGE, initialise);
}
function initialise(e:Event)
{
stage.addEventListener(MouseEvent.CLICK, fire);
}
function fire(m:MouseEvent)
{
//If we are allowed to shoot
if(shotCooldown<=0)
{
//Reset the Shot Cooldown
shotCooldown=MAX_COOLDOWN;
//Spawn a Bullet
var b = new Bullet();
b.rotation = rotation;
b.x = x;
b.y = y;
//Add the bullet to the list of bullets
bullets.push(b);
parent.addChild(b);
play();
}
}
function update(e:Event)
{
//Reduce the Shot Cooldown by 1
//shotCooldown=shotCooldown-1;
//shotCooldown-=1;
shotCooldown--;
if(parent != null)
{
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy, dx)/Math.PI * 180;
rotation = angle;
}
}
}
}
更改4,調整你喜歡 – user3217163
速度對不起隊友,忘了給我的子彈評論as3課。這裏是:package { \t import flash.display.Sprite; \t import flash.events.Event; \t \t公共類子彈擴展Sprite { \t \t //屬性 \t \t私人變種速度:INT; \t \t \t 公共\t功能子彈(){ \t \t速度= 10; \t \t} \t \t功能更新(){ \t \t \t // Retning skuddene斯凱特MOT \t \t X = X + Math.cos(旋轉/ 180 * Math.PI)*速度; y = y + Math.sin(rotation/180 * Math.PI)* speed; \t \t y = y + Math.sin \t \t \t // \t刪除子彈時其關閉屏幕 \t \t如果(X <0 || x> 500 ||ý<0 || y> 500){ \t \t \t //從屏幕 \t \t \t母體取出子彈。removeChild之(本); \t \t \t \t \t \t} \t \t} \t \t} \t \t } – user3276442
我測試你的代碼,它工作得很好,問題是你是不是調用更新功能,嘗試添加一個輸入框項目符號類上的事件監聽器並在其中調用更新。 – user3217163