Я боролся с as3 и флеш-игрой, которую я пытаюсь сделать. Все выглядит прекрасно, но все же пуля застревает внутри пушки. Когда я использовать мышь, чтобы стрелять, вместо того, чтобы выходить на место, он просто застрянет внутри пушки:Ошибка AS3 Turret и bullet
Got 3 AS3 документов, и один флэш-документ:
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
Прошу прощения, забыл прокомментировать класс bullet as3. Вот он: package { \t импорт flash.display.Sprite; \t импорт 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 skytter MOT \t \t х = х + Math.cos (вращение/180 * Math.PI) * скорость; \t \t y = y + Math.sin (вращение/180 * Math.PI) * скорость; \t \t \t \t // Удаляем пуля, когда его с экрана \t \t если (х <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