So now I have another issue :3 This code down here does make the character rotate, but he rotates like a weirdo. If you want to see how he rotates go here:[url=http://megaswf.com/serve/1131412]
atan is the inverse of tangent which normally is represented by tan-1, but for programming uses is represented by atan. So yes, it involves trigonometry.
I just started with my character facing a different direction to yours thats all. I just added +90 to mine to make him face the correct direction.
Also, I would assume you are going to try firing bullets now correct? I have a pretty good idea how to easily achieve that so I can help you with that if you want, and without any of the horrible trig stuff, just pythagoras theorem.
Pythagorean theorem is trigonometry, it's just much simpler then cosine, tangent, and sine. Also, using sine and cosine to have an object move across an angle isn't too difficult.
Pythagorean theorem is trigonometry, it's just much simpler then cosine, tangent, and sine. Also, using sine and cosine to have an object move across an angle isn't too difficult.
I meant no kinda hard trigs. I realise that it is quite easy to do, it just requires a level of concentration. I definitely prefer using the more basic pythagoras theory where applicable so I can sit back and relax rather than spend more effort converting angles and drawing circles and triangles in my head. It also takes a lot less processing power if functions like atan can be avoided.
Yep I'm going to work on bullets now. I did it in as2, but I'm not sure how to do it in as3 :/
You can make an array to store your bullets in, so in your variable declarations for what will parent your bullets you can add; var bullets:Array = []; var bullet:Bullet;
And then, when ever you want to create a bullet, you can put; bullet = new Bullet(); bullets.push(bullet); addChild(bullet);
You can then refer to each bullet by indexes in your bullets array. You can also loop through all the bullets using a loop like so; for(var i:int=0; i<bullets.length; i++) trace(bullets[i]);
In what ever class you plan on making your bullets on. Probably on the same class you made your character on. You can make a new class to handle the functionality of the bullet internaly but the bullet's creation would have to be external.
I'm sorry if I'm just being stupid, but am I using your code incorrectly?
This is what I have in my Character.as:
package{
import flash.display.MovieClip; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; public class Character extends MovieClip{
var agility:Number var rightPressed:Boolean; var leftPressed:Boolean; var upPressed:Boolean; var downPressed:Boolean; var spacePressed:Boolean; var bullets:Array = []; var bullet:Bullet;
function Character(){ agility=5; addEventListener(Event.ENTER_FRAME, move); addEventListener(Event.ADDED_TO_STAGE, addedToStage); character.stop(); } public function addedToStage (e:Event){ removeEventListener(Event.ADDED_TO_STAGE, addedToStage); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); stage.addEventListener(KeyboardEvent.KEY_UP, keyUp); }
public function keyDown(e:KeyboardEvent){ if(e.keyCode==39){ rightPressed = true; }
I don't suggest creating bullets within your character since it will move with your character. You have your variable declarations in the right place and you should have no trouble creating multiple bullets, but I would instead place those in your character's parent.
In the class that contains something like, addChild(character). So in that class, do the variable declarations and bullet creation on there instead. It will also be somewhat easier to make your bullets interact with walls and enemies later this way (assuming you also make enemies and walls in this class).
I usually have a class called PlayScreen at this point, that would hold the character, projectiles, enemies, terrain and other objects. It tends to get more a lot more bulky than the rest of my classes but it makes object interactions much easier to do.
var spacePressed:Boolean; var bullets:Array = []; var bullet:Bullet;
private var character:Character = new Character(); private var bullet:Bullet = new Bullet(); } public function keyDown(e:KeyboardEvent){ if(e.keyCode==49){ spacePressed = true; } public function keyUp(e:KeyboardEvent){ if (e.keyCode == 49) spacePressed = false; } function keyDown(e:KeyboardEvent){ if(spacePressed){ bullet = new Bullet(); bullets.push(Bullet); addChild(bullet); bullet.x=400 bullet.y=30 } }
This is the error:
/Users/Lennon/Desktop/Zombie Epidemic AS3/Game.as, Line 24 1114: The public attribute can only be used inside a package.
You seem to have closed off your Game class directly before your first keyDown function. You also seem to have duplicate keyDown functions, I think the second one will override the first, so you should merge the two.