PDA

View Full Version : shooting spaceship reloadinh help



sam_vickery
August 28th, 2008, 02:09 PM
I have made a little spaceship that moves around and fires. I want to make it so that when it runs out of ammo it starts a relaod timer and when the timer is up it can shoot again. I am a complete novice so any help would be gr8. Also if you want to add anything be my guest.

cheers

dozza92
August 29th, 2008, 12:55 AM
Hey sam_vickery.

I went through your code and you did have a lot of typos in there!
Your timing concept is right but the variables are the wrong way round, it should be:


if (getTimer()-currentTime>=bulletDelay)
this is the same for the reload delay:



if (getTimer()-currentreloadTime>=reloadDelay)
which says that: "if the time elapsed after the time i saved the variable is greater then the amount of time for the bullet to delay, then <rest of code here>"

To put mathematically, it finds the difference from the time saved in the variable and the current time.

Finally your check ammo code should look like this:



function chackammo() {
// check if we are out of ammo
if (ammo>0) {
// if not, just return (exit the function)
return;
}
// otherwise we are out of ammo
// we need to stop and reload, firstly we should tell the
// computer were not reloaded if it thinks we are
if(reloaded)
{
// turn off reloaded
reloaded = false;
// set current time to calculate wait time
currentreloadTime = getTimer();
}
// find the difference between the current time and the time that we saved
// if it is greater than our reload wait time, we can say that we
// have reloaded, and add on extra ammo
if (getTimer()-currentreloadTime>=reloadDelay) {
// add the ammo
ammo = 50;
// and say we have reloaded
reloaded = true;

}
}
The comments should explain well enough.

I will upload the completed fla, but i think you should run through your own code and fix the typos and the errors, to get the hang of debugging. If you do get stuck, refer to my .fla file :).

SparK_BR
August 29th, 2008, 11:53 PM
i think it's a lot simpler

create a var for the delay
subtract one from it every frame loop

when you shot test to see if var is equal or smaller than 0
if yes, then shot and set delay to some greater than 0 value


hmm... code is best for understanding:


onClipEvent(load){
int delay = 0; //creating var to store delay time.
}
onClipEvent(enterFrame){
delay--; //subtracting delay as the frames go by.
}

function shot(){ //call this function to shot.
if(delay <= 0){ //testing delay to see if you can shot or not.
<create shot here> //this is where you create your shot.
delay = <your delay here>; //delay, in frames.
}
}
note: this code assumes infinite ammo.

dozza92
August 30th, 2008, 12:13 AM
i think it's a lot simpler

create a var for the delay
subtract one from it every frame loop

when you shot test to see if var is equal or smaller than 0
if yes, then shot and set delay to some greater than 0 value


hmm... code is best for understanding:
Code:


onClipEvent(load){
int delay = 0; //creating var to store delay time.
}
onClipEvent(enterFrame){
delay--; //subtracting delay as the frames go by.
}
function shot(){ //call this function to shot.
if(delay <= 0){ //testing delay to see if you can shot or not.
<create shot here> //this is where you create your shot.
delay = <your delay here>; //delay, in frames.
}
}

note: this code assumes infinite ammo.
Yes your right, it's much simpler that way however this way was the setup he was trying to accomplish in his fla file, which is why i used it. However Spark_BR's way is much simpler.

sam_vickery
August 30th, 2008, 05:55 PM
thanks for help so far but I was now wandering about enemies. I have created a spike that I would like to fall out of the sky at a random speed and x position. And when the spikes have hit the floor they disappear. Also when a ceratin ammount of spikes have fallen it plays a different frame.

I know its a lot to ask for but I just dont know where to start.


cheers again

dozza92
August 30th, 2008, 07:45 PM
More code for ya sam:



// this function initiates and moves a spike
function Init_Spike()
{
// create a variable that contains a number to count the spikes
// as they are added
spikeCount++;

// attach the spike to the movie - you know how it works.
this.attachMovie("spike","spike"+spikeCount,spikeCount);

// set the properties of the spike, this method accesses the root
// timeline/object as an array which is kinda weird. another way would to be
// to use the eval function.

// set y position to just above the scene
this["spike"+spikeCount]._y = -100;

// set the x position to anywhere along the x axis of the stage
this["spike"+spikeCount]._x = Math.random()*Stage.width;

// set the velocity of the object, velocity isnt a real property however
// flash creates one and initialazes it for you. the correct way of yoing this
// would be to create a class with a property velocity and inherit the
// movieclip class into it.
this["spike"+spikeCount].velocity = Math.random()*20;

// this function is just like any other onEnterFrame function
// but we have set it up for this seperate bullet. Anotherway of implementing this
// would record the name data for the spikes, say in an array, and create one function
// which will cycle through the data and do whatever you want with it. This method
// will inevitably lead to more control.
this["spike"+spikeCount].onEnterFrame = function()
{
// move the spike
this._y += this.velocity;

// remove the clip if it is passed the stage
if(this._y > Stage.height + 200)
this.removeMovieClip();
}
}

:)

sam_vickery
August 31st, 2008, 07:17 AM
More code for ya sam:



// this function initiates and moves a spike
function Init_Spike()
{
// create a variable that contains a number to count the spikes
// as they are added
spikeCount++;

// attach the spike to the movie - you know how it works.
this.attachMovie("spike","spike"+spikeCount,spikeCount);

// set the properties of the spike, this method accesses the root
// timeline/object as an array which is kinda weird. another way would to be
// to use the eval function.

// set y position to just above the scene
this["spike"+spikeCount]._y = -100;

// set the x position to anywhere along the x axis of the stage
this["spike"+spikeCount]._x = Math.random()*Stage.width;

// set the velocity of the object, velocity isnt a real property however
// flash creates one and initialazes it for you. the correct way of yoing this
// would be to create a class with a property velocity and inherit the
// movieclip class into it.
this["spike"+spikeCount].velocity = Math.random()*20;

// this function is just like any other onEnterFrame function
// but we have set it up for this seperate bullet. Anotherway of implementing this
// would record the name data for the spikes, say in an array, and create one function
// which will cycle through the data and do whatever you want with it. This method
// will inevitably lead to more control.
this["spike"+spikeCount].onEnterFrame = function()
{
// move the spike
this._y += this.velocity;

// remove the clip if it is passed the stage
if(this._y > Stage.height + 200)
this.removeMovieClip();
}
}

:)



it didnt seem to work do you think you could add the code for me. I understand how it works but I cant get it to work


thanks

dozza92
August 31st, 2008, 04:02 PM
There ya go sam_vickery.

The fla for the Init_Spike function is attached, theres also a Time_Spike function which is used in the main game loop. It times the spikes to fall, ive explained how it works. I've kept the timing methods the same rather than using Spark_BR's method only becuase thats how you've set up your code.

SparK_BR
August 31st, 2008, 08:36 PM
There ya go sam_vickery.

The fla for the Init_Spike function is attached, theres also a Time_Spike function which is used in the main game loop. It times the spikes to fall, ive explained how it works. I've kept the timing methods the same rather than using Spark_BR's method only becuase thats how you've set up your code.

yeah sam, do what he said :cowboy:

@dozza: nice code!

ps:
@sam: when it's done i will play your game

sam_vickery
September 1st, 2008, 07:32 AM
thanks guys ur both gr8

I have now made a function to check if the spike hits the bullet and when it does the money goes up and the spike gets removed but i cant get it to work here is the code I used

function spikehit()
{spike="spike"+spikecount;bullet="bullet"+bulletcount;
if(spike.hitTest(bullet)){_root.money+=100;spike.r emoveMovieClip()}}


can you please help me


thanks

dozza92
September 2nd, 2008, 04:22 PM
Hello again sam.
The first problem i see with this code is the spaces inbetween the functions and variables - get rid of those:


function spikehit()
{
spike="spike"+spikecount;
bullet="bullet"+bulletcount;
if(spike.hitTest(bullet))
{
_root.money+=100;
spike.removeMovieClip(); // end all code in a semi-colon!
}
}


Secondly, you cannot use variables straight up to reference an instance of somthing. They either need to go through the eval() function or through the _root array selection like I demonstrated on the Init_Spike code. I prefer the latter, however both ways are as good.



function spikehit()
{
// define the spike and bullet names
spike="spike"+spikecount;
bullet="bullet"+bulletcount;

// test spike and bullet collision
// put both spike and bullet variables either in eval() function
// or in the _root array like so:
if(_root[spike].hitTest(_root))
{
_root.money+=100;
[B]_rooot[spike].removeMovieClip(); // end all code in a semi-colon
}
}


That should do it from what you've given me, havent tested it but it looks okay :).

SparK_BR
September 2nd, 2008, 05:30 PM
are the spikecount and bulletcount vars being updated somewhere by a for loop?
or they are _root vars?

i would do something like this:
spikecount would be the num of spikes i have and bulletcount the num of bullets i have
(or the max number of spikes and bullets allowed, which works better but slowly)
then:


function checkSpikeBulletCollision(){
for (int i=0; i<spikecount; i++){
for (int j=0; j<bulletcount; i++){
if(_root["spike"+i].hitTest(_root["bullet"+j])){
_root.money+=100;
_root["spike"+i].removeMovieClip();
_root["bullet"+j].removeMovieClip();
}
}
}
}
then I would call it every frame loop...

sam_vickery
September 3rd, 2008, 05:27 AM
thanks again guys but i keeps saying

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 176: '{' expected
function checkcollision();
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 200: Syntax error.
Total ActionScript Errors: 2 Reported Errors: 2

I dont know what to do could someone help


cheers

SparK_BR
September 3rd, 2008, 09:43 AM
you typed function checkcollision;
while the compiler wanted, function checkcollision{ }
but if it's a call you don't need the function keyword

dozza92
September 3rd, 2008, 10:49 AM
You need to learn to debug efficiantly. When your really stuck, then you should write on the forums. check the syntax of both your function declaration and your call syntax. If you cant see the error in these, you might have to manually count the curly brackets and paranthesis: +1 for '{' and '(' ; -1 for '}' or ')'.

sam_vickery
September 3rd, 2008, 11:58 AM
ok i have got rid of all the errors but the when the bullet hits the spike it doesnt remove the spike or add any money:(

SparK_BR
September 3rd, 2008, 12:04 PM
this can mean:
1st option: instance names are not spikeN and bulletN
2nd option: the _root.spikeCount and _root.bulletCount are undefined
3rd option: there's always a third option!

sam_vickery
September 3rd, 2008, 04:58 PM
still no answer?

do u think u could have a look

cheers



i no im pathetic

sam_vickery
September 6th, 2008, 11:10 AM
any1 help the file is attached in an earlier post?

darkblade3276
April 19th, 2009, 11:13 AM
Alas... I found the problem in your last attachment:

function checkcollision() ;:
It should be:

function checkcollision() {
------also... check instances
__________________
Hope this helps