-
[F8] Storing ammo variable value
Sorry for the quite general title, I found it quite difficult to explain my problem in a brief way.
Anyway my problem.
I'm currently trying to develop a sniper/fps game (original I know) and have got the basic engine pretty much sorted. I've currently got two weapons that I can swap from. I also have an ammo value set up for one of the two weapons (global variable) and this works. When the ammo gets to 0 it goes to a reload frame. However say I wanted this second weapon to have more ammo, without getting rid of the other ammos value. How would you recommend I do this. My current code is:
PHP Code:
weapons.gotoAndStop("weapon1");
_global.Ammo1 = 10
This oviously sets the ammo value to 10.
PHP Code:
target1.head.onPress = function() {
_global.Ammo1 -= 1
trace(_global.Ammo1);
if (_global.Ammo1<=0){
gotoAndStop("reload");
}
When the enemy movie clip is pressed the ammo value goes down 1, when it reaches 0 it goes to the reload frame.
Now this all works, which I was really chuffed about considering I've not been coding that long, and this seems like a huge feat.
Now I have another weapon, and would like to give this gun a seperate ammo value, without disrupting Ammo1's value. The code for weapon 2 so far is:
PHP Code:
on (keyPress "2"){
trace("2 down");
weapons.gotoAndStop("weapon2");
I imagine I'd set a new ammo global variable, say ammo2, but how would I change which global variable to subtract the ammo from when I click the enemy, and also without getting rid of the ammo1 value?
Please feel free to leave a comment if you need this better explained. I will try my best xD All help is hugely appreciated!
George
Also sorry if this is in the wrong forum section I thought it was probably meant to go in this one as it is directly related to game help, although I understand that it could go in the Actionscript forum...
-
Maybe you can adapt this to what you are trying to do...
PHP Code:
target1.head.onPress = function() {
if (_global.inUse == "1") {
_global.Ammo1 -= 1;
if (_global.Ammo1 <= 0) {
gotoAndStop("reload");
}
} else {
_global.Ammo2 -= 1;
if (_global.Ammo2 <= 0) {
gotoAndStop("reload");
}
}
};
PHP Code:
_global.inUse = "1";
weapons.gotoAndStop("weapon1");
_global.Ammo1 = 10;
_global.Ammo2 = 5;
PHP Code:
on (keyPress "2"){
trace("2 down");
_global.inUse = "2";
weapons.gotoAndStop("weapon2");
}
-
get familiar with arrays because for something like this I would strongly suggest using them - using functions instead of scattered code might also be worth a investment to get familar with.
anyway the way I would do it is to use a array for holding all informations for all guns
PHP Code:
var weapons:Array = new Array();//craetes a new Array
if you then want to add weapons and thier attributes you can do it like this:
PHP Code:
weapons.push( {name:"glock", ammo:200,mc:mc_glock, reloadTime:600, rounds:17, bulletsInClip:17, damage:4} );//adds a oject to the array
weapons.push( {name:"sniper", ammo:56,mc:mc_styer, reloadTime:1400, rounds:4, bulletsInClip:4, damage:8} );//another gun
with that you then already have a usefull data container,- now we need some functions
PHP Code:
var current_weapon:Number = 0;//the first item, 0 means first item in array, 1 = 2nd ect.
function switchWeapon(nr){
if (nr != current_weapon){
//do some switch animation...
current_weapon = nr;
trace("you picked the weapon: "+weapons[nr].name);
}
}
function shoot(x,y){
var damage = weapons[current_weapon].damage;
if ( weapons[current_weapon].bulletsInClip > 0){//if the gun is reloaded
weapons[current_weapon].bulletsInClip--;
//some shot animation
//hit test with your objects using X,Y
}else{
trace("clack,.. reload the gun....");
}
}
function reload(){
var amount = Math.min(weapons[current_weapon].rounds , weapons[current_weapon].ammo);
weapons[current_weapon].ammo -= amount;//remove bullets from ammo
weapons[current_weapon].bulletsInClip+=amount;
}
this is rather a example reaon on regarding functions (basicly reuseable and structured code ) and arrays, its no that hard
-
Thank you Dawsonk for your advice, and thanks renderhjs, I will definitely try this out when I get back from school. This has helped me hugely, and looks like it should fix what I'm doing. I've already got quite a lot of the information you've stored in the array, but in my case it's in functions, this should not only help with the ammo, but help tidy up my code!
Thanks again to both of you, I will post how I get on.
-
Sorry for the double post, renderhjs, I have been through your code, which looks to be really useful, as it would be a good idea to store the weapons data in arrays.
I just can't understand your code, this is in no way a fault of yours, I am simply very new to Actionscript, if you could perhaps explain it in more detail (or if anyone could) I would really appreciate it, of course if your too busy I understand, either way I will try to make more sense of it. Thanks all the same!
George
-
maybe we can simply the code I just posted- so it might be a lot easier to understand.
Imagine we only want to store the gun name and the ammo for each gun - sounds easy at first.
first a complete code for that (you just need to copy paste that- should work already)
PHP Code:
var guns:Array = new Array(); guns.push({name:"m66", ammo:6}); guns.push({name:"sniper", ammo:4}); gunNr = 0;
//functions function shoot(x,y){ if ( guns[ gunNr ].ammo > 0){ guns[ gunNr ].ammo--;//minus 1 ammo item trace("bang"); }else{//there is 0 or less ammo left, in other words no ammo at all trace("clack, need to reload"); } updateDisplay();//update the GUI } function updateDisplay(){//function to update the GUI (displaying bullets, gun ect.) trace("\ngun statistics: "); trace("gun selected:\t"+guns[ gunNr ].name); trace("gun's ammo left:\t"+guns[ gunNr ].ammo); } function switchGun(nr){ gunNr = nr; }
//mouse event _root.onMouseDown = function(){ shoot(_xmouse,_ymouse);//call the shoot functions with the parameters x and y }
so how does it work,
1.) the array part:
PHP Code:
var guns:Array = new Array(); guns.push({name:"m66", ammo:6}); guns.push({name:"sniper", ammo:4});
the first line tells Actionscript to create a new array, if you want to write quick code, this will do as well:
its basicly the same
the 2nd and 3rd line tell actionscript to push or add content to the array. What I basicly add to the array is a object- that is written like this:
PHP Code:
{name:"m66", ammo:6}
this is a way that was introduced in Flash MX and it creates a Object (just like String or Number its a Data Type in Flash) - but that in a very comfortable and quick way.
Usually for creating objects with properties (properties means names of the content holders) you need to do something like this:
PHP Code:
object = new Object(); object.ammo = 6; object.name = "m66";
but that´s 3 lines because many elements are repeated, just so that you know if you´d use it this regular way the code would look like this:
PHP Code:
var guns:Array = new Array(); var obj:Object = new Object(); obj.name = "m66"; obj.ammo = 6; guns.push( obj ); obj:Object = new Object();//overwrite the old object obj.name = "m66"; obj.ammo = 6; guns.push( obj );
I guess you agree that this is not a comfortable way to fill an array with objects.
2.) functions
functions are the core of scripting and programming without those its not really programming but rather messing around.
Basicly a function holds lines of code that can be reused. It makes sense to create functions for different kind of actions like
shoot();
reload();
updateGUI();
...
in addition of just reusing code you can pass variables to functions so that you can tweak those lines of code within the functions with additional variables (this is basicly what makes functions so interesting - by adding parameters they can be used in different ways).
the function
[php]function shoot(x,y){
}[/php
for example accepts 2 parameters beeing:
- x (in this case the x- coordinate of your screen)
- y (same for the y- coordinate)
within that function you can than acess those parameters and do math stuff- or anything else.
to call the function simply use the function name followed by a open brace the parameters (if there are any) and a closing brace, like
PHP Code:
shoot(_xmouse,_ymouse);
and if you have a function without paraters call them like this
[php]reload();[php]
hope that clears up some things- or do you still have questions regarding the arrays?
-
offtopic:
here is a function for the shoot that I originally tested - but since it has some tricky stuff I left it out at first
PHP Code:
function shoot(x,y){ if ( guns[ gunNr ].ammo > 0){ guns[ gunNr ].ammo--;//minus 1 ammo item trace("bang"); for (mc in _root){//loop through all elements in the _root level if ( typeof(eval(mc)) == "movieclip"){ var hit = eval(mc).hitTest(x,y,true);//do a hit test on the mc if(hit){ trace("mc: "+eval(mc)._name+" got hit"); eval(mc)._visible = false; } } } } updateDisplay();//update the GUI }
it basicly hitTests on all MovieClips on the _root level if they are hit with the x and y coordinates - and if so makes them invisible.
Maybe it is to some extend usefull to you
-
Thanks so much for taking the time to explain all the code, I now understand it a lot more, however I've put it into the frame where all my actionscript is, and it shows all the
gun statistics:
gun selected:
gun's ammo left:
But with no data, I do already have 2 guns set up, as I've already said, how do I link them with the code, basically where should the code you've written go?
Currently the code for swapping weapons is:
PHP Code:
on (keyPress "1"){
trace("Weapon1");
weapons.gotoAndStop("weapon1");
_global.Gunpower = 20;
_global.GunpowerB = 10;
_global.Ammo1 = 10
}
on (keyPress "2"){
trace("2 down");
weapons.gotoAndStop("weapon2");
_global.Gunpower = 10;
_global.GunpowerB = 5;
}
So how can I link this with the array, I am sorry for appearing really stupid, and asking what are probably really basic questions, but I undertand what your saying, however I can't work out how to bring the array and what I already have (the swapping code) together, any help would be greatly appreciated!
Thanks a lot for all the help you've already given, I have learnt a lot!
-
But with no data, I do already have 2 guns set up, as I've already said, how do I link them with the code, basically where should the code you've written go?
the array is probably not present or accessible - have you declared it before? - it should be:
PHP Code:
var guns:Array = new Array(); guns.push({name:"m66", ammo:6}); guns.push({name:"sniper", ammo:4});
if you did that it should work
Originally Posted by GeorgeB
Currently the code for swapping weapons is:
PHP Code:
on (keyPress "1"){ trace("Weapon1"); weapons.gotoAndStop("weapon1"); _global.Gunpower = 20; _global.GunpowerB = 10; _global.Ammo1 = 10 } on (keyPress "2"){ trace("2 down"); weapons.gotoAndStop("weapon2"); _global.Gunpower = 10; _global.GunpowerB = 5; }
on the first key:
PHP Code:
on (keyPress "1"){ switchGun(0);//first gun equals to 0 (remember arrays start with 0 then 1,2,3,...) }
and the 2nd key
PHP Code:
on (keyPress "2"){ switchGun(1);//first gun equals to 0 (remember arrays start with 0 then 1,2,3,...) }
thats what I call orgnazied
next you need to make sure that some of the
PHP Code:
_global.Gunpower = 10; _global.GunpowerB = 5; ...
stuff gets placed within the switchGun function and within the array for the gun data
so for example the array propably would be like this
PHP Code:
var guns:Array = new Array(); guns.push({name="sniper", frameLabel:"weapon1", Gunpower:20, GunpowerB:10 , ammo:6}); guns.push({name="HK 64", frameLabel:"weapon2", Gunpower:10, GunpowerB:5 , ammo:4});
and the switch function like this
PHP Code:
function switchGun(nr){ gunNr = nr; weapons.gotoAndStop(guns[ nr ].frameLabel ); }
as you can see within the switch function it stops to the frame label that has been stored in the array depending on your current weapon. That´s just a few lines of code and thereby very clean and easy to change or update.
you might be confused by now - with all the code snippets so here is a big summary
PHP Code:
var guns:Array = new Array(); guns.push({name="sniper", frameLabel:"weapon1", Gunpower:20, GunpowerB:10 , ammo:6}); guns.push({name="HK 64", frameLabel:"weapon2", Gunpower:10, GunpowerB:5 , ammo:4}); gunNr = 0;
function switchGun(nr){ gunNr = nr; weapons.gotoAndStop(guns[ nr ].frameLabel ); }
function shoot(x,y){ if ( guns[ gunNr ].ammo > 0){ guns[ gunNr ].ammo--;//minus 1 ammo item var gunPower = guns[ gunNr ].Gunpower; var gunPowerB = guns[ gunNr ].GunpowerB;
trace("bang with "+gunPower+" and "+gunPowerB); }else{//there is 0 or less ammo left, in other words no ammo at all trace("clack, need to reload"); } updateDisplay();//update the GUI }
function updateDisplay(){//function to update the GUI (displaying bullets, gun ect.) trace("\ngun statistics: "); trace("gun selected:\t"+guns[ gunNr ].name); trace("gun's ammo left:\t"+guns[ gunNr ].ammo); trace("gun's gunpower:\t"+guns[ gunNr ].Gunpower); trace("gun's gunpowerB:\t"+guns[ gunNr ].GunpowerB); }
//mouse event _root.onMouseDown = function(){ shoot(_xmouse,_ymouse);//call the shoot functions with the parameters x and y }
and finally on your key- buttons:
PHP Code:
on (keyPress "1"){ switchGun(0);//first gun equals to 0 (remember arrays start with 0 then 1,2,3,...) }
and
PHP Code:
on (keyPress "2"){ switchGun(1);//first gun equals to 0 (remember arrays start with 0 then 1,2,3,...) }
test it again
Last edited by renderhjs; 09-11-2008 at 02:55 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|