turret rotation, click and shoot help [AS3]

7 posts

Flag Post

Hi guys,

im having an issue with a simple game i am creating for Uni, the basics are you have a tank, you can move left and right with the arrow keys the turret rotates to follow the mouse pointer and when i click a bullet will go in the direction of the mouse.

My problem is my lecturer thinks its easier to learn the basics by doing all the coding on the one actionscript rather than do classes. every tutorial i come across uses classes and im struggling to get mine working. Could you have a look at my code and point me in the direction where it is wrong. thanks guys

import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;

var turret:Red = new Red();
addChild(turret);
turret.x = stage.stageWidth / 2;
turret.y = stage.stageHeight – 30;
var speed:int = 10;

var rightArrow:Boolean;
var leftArrow:Boolean;
var playerHalfWidth:Number = turret.width / 2;
var bullets:Array = [];

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
stage.addEventListener(Event.ENTER_FRAME, startGame);
stage.addEventListener(MouseEvent.MOUSE_DOWN, fire);

function KeyDown(event:KeyboardEvent):void {
switch(event.keyCode)
{
case Keyboard.RIGHT: {
rightArrow = true;
break;
}
case Keyboard.LEFT: {
leftArrow = true;
break;
}
}
}
function KeyUp(event:KeyboardEvent):void {
switch(event.keyCode)
{
case Keyboard.RIGHT: {
rightArrow = false;
break;
}
case Keyboard.LEFT: {
leftArrow = false;
break;
}
}
}
function startGame(event:Event):void {
movePlayer();

for(var i:int = 0; i < bullets.length; i++) {
var bullet:Bullet = bullets[i];
bullet.x += 3;
if (bullet.x > stage.stageWidth + 2) {
removeChild(bullet);
bullets.splice(i, 1);
i—
}
}

}

function movePlayer ():void {
if(rightArrow == true) {
turret.x += 5;
if(turret.x > stage.stageWidth – playerHalfWidth) {
turret.x = stage.stageWidth – playerHalfWidth;
}
}

if(leftArrow == true) {
turret.x -= 5;
if(turret.x < playerHalfWidth) {
turret.x = playerHalfWidth;
}
}

var angle_in_rad:Number = Math.atan2(mouseY – turret.y, mouseX – turret.x);
turret.rotation = angle_in_rad * 180/Math.PI;
}
function fire(me:MouseEvent):void {
var b:Bullet = new Bullet();
var dx:Number = mouseX – turret.x;
var dy:Number = mouseY – turret.y;
var angle_in_rad:Number = Math.atan2(dy, dx);
b.x = turret.x +dx;
b.y = turret.y +dy;
b.rotation = angle_in_rad * 180/Math.PI;
addChild(b)
bullets.push(b)
}

 
Flag Post

What exactly is the problem? You didn’t specify what was wrong with the code, i.e. is it not shooting? Also, post your code on a site like pastebin.com so it preserves formatting and is much easier to read.

EDIT: Also, just a tip that doesn’t relate to your problem: instead of if(leftArrow == true), you can achieve the same effect by using if(leftArrow)

 
Flag Post

Your lecturer is lazy and doesn’t want to open multiple files to grade his students’ projects. It’s easier to learn to do things properly from the get-go, use classes. He probably wouldn’t think much of a shop teacher who would teach his students to hit nails with the wrench because it’s easier for beginners than having to switch tools all the time.

 
Flag Post

All the tutorials you find using classes should pretty well port directly to the timeline sans the access modifiers. Code on the timeline is converted to a class like structure on compilation. Really, you are creating a class, you just don’t know it.

 
Flag Post

Can’t you theoretically put multiple Classes in one .AS file?

package{
     public Class Turret extends Sprite{
          ...
     }
     public Class Enemy extends Sprite{
          ...
     }
}
 
Flag Post

Yes BUT only one is publicly accessible. The accessory classes go outside the package block.

 
Flag Post
Originally posted by feartehstickman:

Can’t you theoretically put multiple Classes in one .AS file?

package{
     public Class Turret extends Sprite{
          ...
     }
     public Class Enemy extends Sprite{
          ...
     }
}

FD yells “blahblah, can’t have 2 exports from 1 file”. So if 2 classes are marked “public”, an error is thrown. If there’s a “public class” and “class”, it’s allowed.