Cant open your file as using flash mx 2004...heres the general idea ...basically first what you need to know is the rotation value of the cannon
e.g
var angle:Number = cannon_mc._rotation; //this gives answer in degrees
var radians:Number = Math.PI/180 * angle //this is the angle in radians
//now lets place the bullet at the base of the cannon
bullet_mc._x = cannon_mc._x;
bullet_mc._y = cannon_mc._y;
//now we need to define a speed that the bullet will travel at
var bulletSpeed:Number = 50;
//now we need to fire the bullet in the right direction lets define the x and y velocity components
var vx:Number = bulletSpeed*Math.cos(radians);
var vy:Number = bulletSpeed*Math.sin(radians);
//now the rest is simple just put the stuff in an onEnterFrame or setInterval //timer so that the bullet actually moves so a fireBullet function may look something like this(after putting most of the above code in it)
Code:
function fireBullet():Void{
var angle:Number = cannon_mc._rotation;
var radians:Number = Math.PI/180 * angle;
var vx:Number = bulletSpeed*Math.cos(radians);
var vy:Number = bulletSpeed*Math.sin(radians);
_root.onEnterFrame = function(){
bullet_mc._x +=vx;
bullet_mc._y +=vy;
}
}
ive knocked up a very quick fla for you to show you this in action ( although i must say as its so rushed, dont think of it as the best technique etc, as i havent even deleted the onEnterFrame function etc..but it should put you in the right direction, no pun intended lol) Note : Space bar to fire, top/bottom cursor keys to rotate the cannon..at present it will only fire once..
Last edited by silentweed; 03-06-2006 at 08:23 PM.
Basically this is a timer ( which can be used instead of onEnterFrame ) I prefer using timers because their timing is not timed to the frame rate.
Every 1000/24 milliseconds i.e every 1/24 sec the timer runs the function mover() which then runs the For Loop. What this loop does is control the movement of each of these bullets. It loops through the bullet array and moves each bullet to a new position according to what its vx and vy velocity components are
Hope that makes sense
Last edited by silentweed; 03-07-2006 at 08:17 PM.
The variable "nMover" is an interval ID. Flash stores all intervals created using the setInterval() global function in an array. This array can only be accessed through the setInterval() and clearInterval() global functions. Think of setInterval() as an equivelant of the Array.push() method -- it inserts a new interval at the end of the interval array. Think of clearInterval() as an equivelant of the Array.pop() method -- it removes the last interval from the interval array.
Interval IDs store the array index of their respective intervals (all indexes are positive integers greater than zero) rather than the object reference, such that supplying the interval ID to the clearInterval() global method effectively removes the interval that corresponds with that interval ID.
Intervals are used to invoke functions at a rate that is seperate from the framerate specified in the document settings and is not reliant upon other forms of loops, such as "for," "for ... in," or "while."
The function "mover" is being invoked at a rate of 24 frames per second, because one second contains 1,000 milliseconds and dividing this by 24 returns approximately 42 milliseconds, or 24 fps.
Ideally, setInterval() would be passed its own interval ID so that the function being invoked could cancel the interval calling it. As in:
Code:
var nMover:Number = setInterval(mover, (1000/24), nMover);
function mover(stopCallingMe:Number):Void {
// pesudo-code:
movement code goes here...
if((bullet has hit enemy) || (bullet can no longer be seen)) {
remove the bullet from the array
}
if(there are no more bullets) {
clearInterval(stopCallingMe);
}
};
Hope that makes more sense,
+Q__
Qwai•zang \kwî-'zan\ n [origin unknown] 1 : abstract designer, esp. of complex real-time experiments, c. 21st century
In my "Publish Settings" I am running Flash Player 6 as that is what my original movie was made in. Can anyone help me convert this script into a format that will be accepted?
Ok that now works. Now I am running into a problem where if I try to make the bullet_mc's _x and _y = a symbol that is inside of a symbol, the bullet just shoots across the top of the screen from the left corner to the right. Here is the script that I am using.
Code:
var vr:Number = 5;
var vBullet:Number = 50;
cannon_mc._rotation = -45;
var bulletArray:Array = new Array();
var bulletNumber:Number = 0;
var keyListener:Object = new Object();
Key.addListener(keyListener);
keyListener.onKeyDown = function():Void{
if(Key.getCode() == 32){
fireBullet();
}
}
function fireBullet():Void{
var bullet_mc:MovieClip = _root.attachMovie("bullet_mc", "bullet"+bulletNumber, _root.getNextHighestDepth());
var angle:Number = _root.character.shootable._rotation;
var radians:Number = Math.PI/180 * angle;
bullet_mc.vx = vBullet*Math.cos(radians);
bullet_mc.vy = vBullet*Math.sin(radians);
bulletArray.push(bullet_mc);
bulletNumber++;
bullet_mc._x = _root.character.shootable._x;
bullet_mc._y = _root.character.shootable._y;
}
var nMover:Number = setInterval(mover, 1000/24);
function mover():Void{
for(var i:Number=0; i<bulletArray.length; i++){
_root["bullet"+i]._x +=_root["bullet"+i].vx;
_root["bullet"+i]._y +=_root["bullet"+i].vy;
}
}
EDIT: Also, how could I make the bullet fire when a button is pressed? I tried copying the firebullet(); function into the on (release) statement, but nothing happened.
Sorry for the questions,
Mr_Welfare
Last edited by Mr_Welfare; 07-21-2006 at 11:47 AM.