I had some AS3 code that I wanted to move to the timeline rather than having an external file but it's not working at all, whereas it did in the .as file:
public class EnemyShip extends MovieClip
{
var speed:Number;
var shot = new ShotSound();
function EnemyShip()
{
this.x = 800;
this.y = Math.random() * 275 + 75;
speed = Math.random()*5 + 9;
addEventListener("enterFrame", enterFrame);
addEventListener(MouseEvent.MOUSE_DOWN, mouseShoot);
}
function enterFrame(e:Event)
{
this.x -= speed;
if(this.x < -100)
{
removeEventListener("enterFrame", enterFrame);
Main.gameLayer.removeChild(this);
}
}
function kill()
{
var explosion = new Explosion();
Main.gameLayer.addChild(explosion);
explosion.x = this.x;
explosion.y = this.y;
removeEventListener("enterFrame", enterFrame);
Main.gameLayer.removeChild(this);
Main.updateScore(1);
shot.play();
}
function mouseShoot(event:MouseEvent)
{
kill();
}
That is the code and I've tried adapting it to work in the timeline but nothing happens. I tried adding the code to the EnemyShip movieclip itself as well as adding it to the in game scene but it doesn't work. Any suggestions?
This is a bit sad, using an external class is a cleaner way to do things, but you decide.
Remove every class wrapper and put this code on the first frame of your symbol:
var speed:Number;
var shot = new ShotSound();
this.x = 800;
this.y = Math.random() * 275 + 75;
speed = Math.random()*5 + 9;
addEventListener("enterFrame", enterFrame);
addEventListener(MouseEvent.MOUSE_DOWN, mouseShoot);
function enterFrame(e:Event)
{
this.x -= speed;
if(this.x < -100)
{
removeEventListener("enterFrame", enterFrame);
Main.gameLayer.removeChild(this);
}
}
function kill()
{
var explosion = new Explosion();
Main.gameLayer.addChild(explosion);
explosion.x = this.x;
explosion.y = this.y;
removeEventListener("enterFrame", enterFrame);
Main.gameLayer.removeChild(this);
Main.updateScore(1);
shot.play();
}
function mouseShoot(event:MouseEvent)
{
kill();
}
Related
i'm about to finish my project for University. But I'm stuck with the hittestobject.
var Player: gun = new gun();
Player.x = mouseX;
Player.y = mouseY;
addChild(Player);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
stage.addEventListener(MouseEvent.MOUSE_UP, release_shoot);
function mousemove(e: MouseEvent): void
{
Player.x = mouseX + 200;
Player.y = mouseY + 35;
}
function shoot(event: Event): void
{
var Bullet: bullet = new bullet();
/*var explosion:explo1 = new explo1(); */
Bullet.x = Player.x;
Bullet.y = Player.y;
/* explosion.x = Player.x;
explosion.y = Player.y;*/
Player.rotationX = 5;
Player.rotationY = 5;
addChild(Bullet);
/* addChild(explosion);*/
Bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
}
function release_shoot(event: Event): void
{
var explosion: explo1 = new explo1();
Player.rotationX = -5;
Player.rotationY = -5;
}
function moveBullet(e: Event): void
{
e.target.y -= 12;
e.target.x -= 96;
if (e.target.y <= -200 || e.target.x <= -200)
{
e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(e.target));
}
}
function goesside_1(event: Event): void
{
mc_target.x -= 2;
if (mc_target.x < -20)
mc_target.x = 550;
}
mc_target.addEventListener(Event.ENTER_FRAME, goesside_1);
function targeting(event: Event): void
{
var bullet: MovieClip = MovieClip(event.target);
if (bullet.hitTestObject(mc_target))
{
mc_burst.x = mc_target.x;
mc_burst.y = mc_target.y;
mc_burst.gotoAndPlay(2);
mc_target.x = 200;
mc_target.removeEventListener(Event.ENTER_FRAME, targeting);
mc_target.x = 200;
trace("targerting");
}
else if (mc_target.x > 550)
bullet.removeEventListener(Event.ENTER_FRAME, targeting);
else
bullet.y -= 12;
bullet.x -= 96;
}
The bullet is going in the Target without any doubt, I see it haha... But won't replace mc_target with mc_burst.
EDIT
This is the working code I used for anyone who's interested:
var Player:gun = new gun();
Player.x = mouseX;
Player.y = mouseY;
addChild(Player);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
stage.addEventListener(MouseEvent.MOUSE_UP, release_shoot);
function mousemove(e:MouseEvent):void{
Player.x = mouseX + 200;
Player.y = mouseY + 35;
}
function shoot(event:Event):void{
var bullet1:bullet = new bullet();
/*var explosion:explo1 = new explo1(); */
bullet1.x = Player.x;
bullet1.y = Player.y;
/* explosion.x = Player.x;
explosion.y = Player.y;*/
Player.rotationX = 5;
Player.rotationY = 5;
addChild(bullet1);
/* addChild(explosion);*/
bullet1.addEventListener(Event.ENTER_FRAME, targeting);
}
function release_shoot(event:Event):void{
var explosion:explo1 = new explo1();
Player.rotationX =- 5;
Player.rotationY =- 5;
}
function movebullet(e:Event):void{
e.target.y -= 12;
e.target.x -=96;/*When the function is called the targets Y position will be subract by 40 pixels every frame, this makes the movieclip move up. The target is the Bullet movieclip.*/
if(e.target.y <= -200 && e.target.x <= -200 ){
e.target.removeEventListener(Event.ENTER_FRAME, movebullet);
removeChild(MovieClip(e.target));
}
}
function goesside_1(event:Event):void {
mc_target.x -= 2;
if (mc_target.x < -20)
mc_target.x = 550;
}
mc_target.addEventListener(Event.ENTER_FRAME, goesside_1);
function targeting(event:Event):void {
var bullet1:MovieClip = MovieClip(event.target);
if (bullet1.hitTestObject(mc_target)){
mc_burst.x = mc_target.y;
mc_burst.y = mc_target.x;
mc_burst.gotoAndPlay(2);
mc_target.x = 200;
mc_target.removeEventListener(Event.ENTER_FRAME, targeting);
mc_target.x = 200;
trace("targerting");
}
else if (mc_target.x > 550){
bullet1.removeEventListener(Event.ENTER_FRAME, targeting);
}
else{
bullet1.y -= 12;
bullet1.x -= 96;}
}
// REPLACING CURSOR BY A SIGHT //
import flash.ui.Mouse;
Mouse.hide();
var myCursor:sight = new sight();
myCursor.visible = false;
function init()
{
addChild(myCursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
stage.addEventListener(MouseEvent.ROLL_OVER, mouseLeaveHandler);
stage.addEventListener(MouseEvent.ROLL_OUT, mouseMoveHandler);
}
function mouseMoveHandler(evt:MouseEvent):void
{
myCursor.visible = true;
myCursor.x = evt.stageX + 10;
myCursor.y = evt.stageY + 10;
}
function mouseLeaveHandler(evt:Event):void
{
myCursor.visible = false;
}
init();
Assuming the code you've posted is everything, the issue is that the targeting method is never called.
Seems like you want to add it as a handler for the enter frame event (as you are removing a listener to that end inside the method)
eg.
bulletInstance.addEventListener(Event.ENTER_FRAME, targeting);
That said, looking at your code, you're going to want to combine your move and targeting functions (you don't want to keep collision checking after you've removed the bullet in your moveBullet function) - or at least remove the targeting enter frame listener when you remove the button from the screen.
Possibly something like this:
function removeBullet(b:MovieClip):void {
b.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(b));
}
function moveBullet(e:Event):void {
var bullet:MovieClip = MovieClip(event.target);
bullet.y -= 12;
bullet.x -= 96;
if(bullet.y <= -200 || bullet.x <= -200 ){
removeBullet(bullet);
}
if (bullet.hitTestObject(mc_target)){
mc_burst.x = mc_target.x;
mc_burst.y = mc_target.y;
mc_burst.gotoAndPlay(2);
mc_target.x = 200;
removeBullet(bullet);
trace("targerting");
} else if (mc_target.x > 550){
removeBullet(bullet);
}
}
If you have many bullets, you'll probably want to have just one enter frame handler, and iterate through each bullet there - instead of having a separate enter frame handler for each bullet.
Also, I'm surprised you are not getting errors, because you have ambiguous naming going on. You have a class called bullet, but then you create vars called bullet as well. Standard practice in AS3 is the give your class names a capitol first letter, and your instance names a lowercase first letter. I'd recommend you do this to avoid errors and ambiguous code.
I would like to thanks BadFeelingAboutThis for his fast help here.
So for people who want to use my code, go head it works now
var Player:gun = new gun();
Player.x = mouseX;
Player.y = mouseY;
addChild(Player);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
stage.addEventListener(MouseEvent.MOUSE_UP, release_shoot);
function mousemove(e:MouseEvent):void{
Player.x = mouseX + 200;
Player.y = mouseY + 35;
}
function shoot(event:Event):void{
var bullet1:bullet = new bullet();
/*var explosion:explo1 = new explo1(); */
bullet1.x = Player.x;
bullet1.y = Player.y;
/* explosion.x = Player.x;
explosion.y = Player.y;*/
Player.rotationX = 5;
Player.rotationY = 5;
addChild(bullet1);
/* addChild(explosion);*/
bullet1.addEventListener(Event.ENTER_FRAME, targeting);
}
function release_shoot(event:Event):void{
var explosion:explo1 = new explo1();
Player.rotationX =- 5;
Player.rotationY =- 5;
}
function movebullet(e:Event):void{
e.target.y -= 12;
e.target.x -=96;/*When the function is called the targets Y position will be subract by 40 pixels every frame, this makes the movieclip move up. The target is the Bullet movieclip.*/
if(e.target.y <= -200 && e.target.x <= -200 ){
e.target.removeEventListener(Event.ENTER_FRAME, movebullet);
removeChild(MovieClip(e.target));
}
}
function goesside_1(event:Event):void {
mc_target.x -= 2;
if (mc_target.x < -20)
mc_target.x = 550;
}
mc_target.addEventListener(Event.ENTER_FRAME, goesside_1);
function targeting(event:Event):void {
var bullet1:MovieClip = MovieClip(event.target);
if (bullet1.hitTestObject(mc_target)){
mc_burst.x = mc_target.y;
mc_burst.y = mc_target.x;
mc_burst.gotoAndPlay(2);
mc_target.x = 200;
mc_target.removeEventListener(Event.ENTER_FRAME, targeting);
mc_target.x = 200;
trace("targerting");
}
else if (mc_target.x > 550){
bullet1.removeEventListener(Event.ENTER_FRAME, targeting);
}
else{
bullet1.y -= 12;
bullet1.x -= 96;}
}
// REPLACING CURSOR BY A SIGHT //
import flash.ui.Mouse;
Mouse.hide();
var myCursor:sight = new sight();
myCursor.visible = false;
function init()
{
addChild(myCursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
stage.addEventListener(MouseEvent.ROLL_OVER, mouseLeaveHandler);
stage.addEventListener(MouseEvent.ROLL_OUT, mouseMoveHandler);
}
function mouseMoveHandler(evt:MouseEvent):void
{
myCursor.visible = true;
myCursor.x = evt.stageX + 10;
myCursor.y = evt.stageY + 10;
}
function mouseLeaveHandler(evt:Event):void
{
myCursor.visible = false;
}
init();
I want to make a top down strategy game in as3, and I want the user to be able to mark multiple movieclips with the mouse, like in operating systems where ju press the mouse button down and then drag the mouse, and it will create a rectangle. How do you do that in as3?
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
var sp : Sprite = new Sprite();
var p1 : Point = new Point();
var p2 : Point = new Point();
function onDown(e:MouseEvent) : void {
p1.x = mouseX;
p1.y = mouseY;
addEventListener(Event.ENTER_FRAME, onMove);
}
function onMove(e:Event) : void {
p2.x = mouseX;
p2.y = mouseY;
draw();
}
function onUp(e:MouseEvent) : void {
removeEventListener(Event.ENTER_FRAME, onMove);
stage.removeChild(sp);
}
function draw() : void {
sp.graphics.clear();
p2.x = p2.x - p1.x;
p2.y = p2.y - p1.y;
sp.graphics.lineStyle(1, 0x0000FF);
sp.graphics.beginFill(0xC2C2C2, 0.2);
sp.graphics.drawRect(p1.x, p1.y, p2.x, p2.y);
stage.addChild(sp);
}
Hope this helps...
You can do like that (copy and paste the following code into your Actions panel):
var s:Shape = new Shape();
s.graphics.beginFill(0xC2C2C2, 0.2);
s.graphics.lineStyle(0, 0x666666);
s.graphics.drawRect(0, 0, 100, 100);
function mouseDownHandler(e:MouseEvent):void {
addChild(s);
s.x = e.stageX;
s.y = e.stageY;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
function enterFrameHandler(e:Event):void {
s.scaleX = (mouseX - s.x) / 100;
s.scaleY = (mouseY - s.y) / 100;
}
function mouseUpHandler(e:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
removeChild(s);
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
It's nice to have a simple selection box, but how about some selectable objects?
Have a look at what I've got, it's quite simple, and most of the code is for the visuals.
// Add 100 green circles.
var objects:Vector.<Sprite> = new Vector.<Sprite>();
for(var i:int = 0; i < 100; ++i){
var object:Sprite = new Sprite();
object.x = Math.random() * stage.stageWidth;
object.y = Math.random() * stage.stageHeight;
object.graphics.lineStyle(2, 0x00FF00);
object.graphics.beginFill(0x00FF00, 0.5);
object.graphics.drawCircle(0,0,15);
object.graphics.endFill();
objects.push(object);
addChild(object);
}
// Main variables we'll be using.
var startPos:Point = new Point();
var selection:Rectangle = new Rectangle();
var isSelecting:Boolean = false;
// Mouse listeners.
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mDown(e:MouseEvent):void {
startPos.x = stage.mouseX;
startPos.y = stage.mouseY;
isSelecting = true;
}
function mUp(e:MouseEvent):void {
isSelecting = false;
}
// Main loop; check if we're intersecting with any of the sprites; if so render them yellow.
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event):void {
graphics.clear();
if(isSelecting) {
selection = new Rectangle(startPos.x, startPos.y, stage.mouseX - startPos.x, stage.mouseY - startPos.y);
normalize(selection);
graphics.lineStyle(2, 0xFF0000);
graphics.beginFill(0xFF0000, 0.5);
graphics.drawRect(selection.x, selection.y, selection.width, selection.height);
graphics.endFill();
for (var j:int = 0; j < objects.length; ++j) {
var object:Sprite = objects[j];
if(selection.intersects(object.getBounds(this))){
object.graphics.clear();
object.graphics.lineStyle(2, 0xFFCC00);
object.graphics.beginFill(0xFFCC00, 0.5);
object.graphics.drawCircle(0,0,15);
object.graphics.endFill();
}
}
}
}
// Intersection requires positive values.
function normalize(rect:Rectangle):void {
if (rect.width < 0) {
rect.width = -rect.width;
rect.x -= rect.width;
}
if (rect.height < 0) {
rect.height = -rect.height;
rect.y -= rect.height;
}
}
What code should I put in here to make my ship stay in the stage?
My ship keeps going off the stage when I press the keys and it goes too far. How do I get it to bump into walls and make it stay on the stage?
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
public class Ship extends MovieClip{
var velocity:Number;
var shootLimiter:Number;
var health:Number;
var maxHealth:Number;
function Ship(){
velocity = 10;
shootLimiter = 0;
health = 100;
maxHealth = 100;
addEventListener("enterFrame", move);
}
function kill(){
var explosion = new Explosion();
stage.addChild(explosion);
explosion.x = this.x;
explosion.y = this.y;
removeEventListener("enterFrame", move);
this.visible = false;
Game.gameOver();
}
function takeDamage(d){
health -= d;
if(health<=0){
health = 0;
kill();
}
Game.healthMeter.bar.scaleX = health/maxHealth;
}
function move(e:Event){
shootLimiter += 1;
if(Key.isDown(Keyboard.D)){
this.x = this.x + velocity;
}
if(Key.isDown(Keyboard.A)){
this.x = this.x - velocity;
}
if(Key.isDown(Keyboard.W)){
this.y = this.y - velocity;
}
if(Key.isDown(Keyboard.S)){
this.y = this.y + velocity;
}
if(Key.isDown(Keyboard.SPACE) && shootLimiter > 8){
shootLimiter = 0;
var b = new Bullet();
stage.addChild(b);
b.x = this.x + 40;
b.y = this.y + -25;
}
if(shield.visible == true){
shield.alpha -= 0.0005;
if(shield.alpha == 0){
shield.visible = false;
shield.alpha = 1;
}
}
}
}
}
Your code is hard to fix as it is, but I'll give you an answer in pseudo, so you could think on how to implement it to fit in your project. Your ship is going out of the frame because it has no notion of where to stop. Lets say the ship x and y coordinates are taken from the upper left corner of the sprite, as it is in actionscript, then, every time your ship moves to the left, you must consider the following:
if(Key.isDown(Keyboard.A)){
var futureX:Number = this.x - velocity;
if (futureX <= 0){ //your ship has reached the left border of the screen
this.x = 0;
} else {
this.x = futureX;
}
}
things get a little more complicated when you're checking for the right or bottom border of the screen, as you need to consider the size of your stage and the size of your sprite in the calculations:
if(Key.isDown(Keyboard.S)){
var futureY:Number = this.y + velocity;
if (futureY + this.height >= Stage.stageHeight){
this.y = Stage.stageHeight - this.height;
} else {
this.y = futureY;
}
}
The solution for the top and right borders are trivial after this.
curious to know if I can somehow make the enemy movie clip in my flash game follow the movie clip the player controls, called 'player' in this instance. At the minute, it's just set to walk -3 on X and 1.5 on Y.
Not really sure if I need to post any code on here, just in case, here is my code inside the Enemy Movie clip.
Thanks in advance!
import flash.events.MouseEvent;
var catxSpeed:Number = -3;
var catySpeed:Number = 1.5;
var myParent:*;
var ground:Number;
var jumping:Boolean = false;
var health:Number;
this.addEventListener(Event.ENTER_FRAME, updatecat);
function updatecat(event:Event):void
{
this.x += catxSpeed;
if(myParent.player.hitTestObject(this))
{
myParent.hit();
}
this.y += catySpeed;
if(jumping == true)
{
catySpeed +=1;
if(this.y >= ground)
{
catxSpeed = -3;
catySpeed = 0;
this.y = ground;
jumping = false;
if(health < 1)
{
shutDown();
}
}
}
}
function activate(passParent:*):void
{
ground = this.y;
this.addEventListener(Event.ENTER_FRAME, updatecat);
this.addEventListener(MouseEvent.MOUSE_DOWN, hit);
myParent = passParent;
health = Math.round(myParent.randomise(2,4));
}
function shutDown():void
{
this.removeEventListener(Event.ENTER_FRAME, updatecat);
this.parent.removeChild(this);
}
function hit(event:MouseEvent):void
{
catxSpeed = 30;
catySpeed = -15;
jumping = true;
myParent.addToScore(1);
health--;
}
if (enemy.x > this.x) this.x += catxSpeed;
else this.x -= catxSpeed;
if (enemy.y > this.y) this.y += catySpeed;
else this.y -= catySpeed;
Ok, let me start off by saying that this is my first AS3 project. I'm ok with Java, so OOP and inheritance aren't new to me.
I have literally spent hours sitting here and pondering why my code isn't working like I wanted it to.
I'm starting off with a turret trying to shoot a bullet. However, the bullet isn't being drawn onto the screen, even though the x and y coords are still changing.
The method where the bullet/projectile is created is called OnMouseClick.
Here's my code:
public class Main extends Sprite
{
public var projectileArr : Array = new Array(); //array which the projectile objects are stored
public var projCount : Number = 0; //projectile counter
public var curX:Number; //mouse coords
public var curY:Number;
private var tri1:Sprite = new Sprite();
private var tri1Height:Number = 50;
private var rect1:Sprite = new Sprite();
private var rect1W:Number = 2;
private var rect1H:Number = 10;
private var angle:Number = 0;
private var acceleration:Number = 0;
public var triSpeedX:Number = 0;
public var triSpeedY:Number = 0;
public var turretAngle:Number ;
//keyboard booleans
private var leftDown:Boolean = false, rightDown:Boolean = false, upDown:Boolean = false, downDown:Boolean = false;
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
//draw triangle
addChild(tri1);
tri1.graphics.lineStyle(1, 0xff00ff00);
tri1.graphics.beginFill(0xff0000);
tri1.graphics.moveTo(0, -tri1Height / 2)
tri1.graphics.lineTo(tri1Height / 3, +tri1Height / 2);
tri1.graphics.lineTo(-tri1Height / 3, +tri1Height / 2);
tri1.graphics.endFill();
tri1.x = 400;
tri1.y = 300;
//draw turret
addChild(rect1);
rect1.graphics.beginFill(0xFFFFFF);
rect1.graphics.moveTo(rect1W / 2, -rect1H);
rect1.graphics.lineTo(rect1W / 2, 0);
rect1.graphics.lineTo(-rect1W / 2, 0);
rect1.graphics.lineTo(-rect1W / 2, -rect1H);
rect1.graphics.endFill();
rect1.x = tri1.x;
rect1.y = tri1.y;
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(Event.ENTER_FRAME, Run);
stage.addEventListener(MouseEvent.MOUSE_MOVE, getMouseCoord);
stage.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseClick);
}
//creation of projectile on click
public function OnMouseClick (e:MouseEvent):void
{
projectileArr[projCount] = new Projectile (rect1.x, rect1.y, turretAngle); //create a new projectile object
addChild(projectileArr [projCount]); //... then it doesn't display
projCount++;
trace ("BAM");
}
public function getMouseCoord(e:MouseEvent):void
{
curX = mouseX;
curY = mouseY;
}
public function onUp(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.LEFT)
leftDown = false;
else if (e.keyCode == Keyboard.RIGHT)
rightDown = false;
if (e.keyCode == Keyboard.UP)
upDown = false;
else if (e.keyCode == Keyboard.DOWN)
downDown = false;
}
public function onDown(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.LEFT)
leftDown = true;
else if (e.keyCode == Keyboard.RIGHT)
rightDown = true;
if (e.keyCode == Keyboard.UP)
upDown = true;
else if (e.keyCode == Keyboard.DOWN)
downDown = true;
}
public function addProjToScreen (projectile:Projectile) : void
{
addChild(projectile);
}
private function Run(e:Event):void
{
acceleration = 0; //resets acceleration back to 0 to limit speed
if (leftDown)
{
angle -= 5;
}
else if (rightDown)
{
angle += 5;
}
if (upDown)
{
acceleration += 3;
}
else if (downDown)
{
acceleration -= 3;
}
triSpeedX += acceleration * Math.cos(angle * Math.PI / 180);
triSpeedY += acceleration * Math.sin(angle * Math.PI / 180);
tri1.x += triSpeedX;
tri1.y += triSpeedY;
//borders
if (tri1.x < 0)
{
triSpeedX *= -.5;
tri1.x = 0 ;
}
if (tri1.x > 800 )
{
tri1.x = 800;
triSpeedX *= -.5;
}
if (tri1.y < 0)
{
triSpeedY *= -.5;
tri1.y = 0 ;
}
if (tri1.y > 600)
{
triSpeedY *= -.5;
tri1.y = 600;
}
//friction
triSpeedX *= 0.95
triSpeedY *= 0.95
rect1.x = tri1.x;
rect1.y = tri1.y;
var xCurDist:Number = curX - rect1.x;
var yCurDist:Number = curY - rect1.y;
turretAngle = Math.atan(yCurDist / xCurDist) * 180 / Math.PI ;
if (xCurDist < 0 )
{
turretAngle += 180;
}
if (xCurDist > 0 && yCurDist < 0 )
{
turretAngle += 360;
}
rect1.rotation= turretAngle + 90; // + 90 to account for the sprite originally pointing up, which is 270 degrees ccw
tri1.rotation = angle + 90;
for (var i:int = 0 ; i < projCount ; i ++)
{
projectileArr [i].move();
}
}
}
and the Projectile class:
public class Projectile extends MovieClip
{
private var projectile : Sprite = new Sprite();
private var speed: int = 10;
private var angle :Number;
private var xIncr: Number;
private var yIncr: Number;
public function Projectile(x:Number, y:Number, angle:Number)
{
this.x = x;
this.y = y;
this.angle = angle;
projectile.graphics.beginFill (0xFF0000);
projectile.graphics.drawCircle (x, y, 4);
projectile.graphics.endFill() ;
xIncr = speed * Math.cos (angle); // angle needs to be converted to radians
yIncr = speed * Math.sin (angle);
}
public function move () :void
{
trace (this.x + " " + this.y);
this.x += xIncr;
this.y += yIncr;
}
}
I just don't know what I'm missing... I'm calling addChild to all of my newly created objects, what else is there to do?
You are not adding the sprite projectile to the displaylist of Projectile in your constructor.
addChild(projectile);
Also you are doubling up on x and y co-ordinates in your projectile class. Rectify this by changing projectile.graphics.drawCircle (x, y, 4); to projectile.graphics.drawCircle (0, 0, 4);