Adobe Dreamweaver Forums



Last 10 THreads :         Point to file bug in CS4 still (Last Post : msc-photoAndWeb - Replies : 1 - Views : 7 )           »          First Frame field (Last Post : Laer2 - Replies : 2 - Views : 3 )           »          Extension Manager CS4 not installing extensions inDreamweaver CS4 (Last Post : chungmx5 - Replies : 15 - Views : 16 )           »          Inserts are not in same lines (Last Post : Walt F. Schaefer - Replies : 3 - Views : 4 )           »          FLASH DATA in SCORM (Last Post : v_ell - Replies : 0 - Views : 1 )           »          titleWindow nightmare (Last Post : Mitek17 - Replies : 3 - Views : 4 )           »          How to communicate between clients? (Last Post : Amy Blankenship - Replies : 11 - Views : 12 )           »          CS4: CSS export not working for text with image (Last Post : Jim Babbage .:CMX:. & .:Adobe Community Expert:. - Replies : 9 - Views : 12 )           »          why doesn't my form work? (Last Post : CreepyCove - Replies : 0 - Views : 1 )           »          can you make a LineChart cumulative? (Last Post : Amy Blankenship - Replies : 1 - Views : 2 )           »         


Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
User Info Statistics
Go Back   Adobe Dreamweaver Forums > Other Macromedia/Adobe Products > Flash > Actionscript
 
Tags:



Reply
  #1 (permalink)  
Old 10-11-2008, 10:19 PM
coolburn
 
Posts: n/a
Diggs:
Default Limited Ammo

I have a shooting game that I would like to limit the ammo and have the game
end after the last shot is fired. I'm new to using actionscript and I'm using
flash 5. The game is an example used in a book that I have but it doesn't
include how to limit the ammo. I have attached the code that is used on a frame
in the main timeline. The rest of the code is in a movieclip and button that
are placed off the stage. Thank you for any help you can give me with this
problem.


coolburn

function initGame() {
// init balloon variables
nextBalloonTime = 0;
nextBalloon = 0;
balloons = [];
numBalloons = 40;

// init bullet variables
nextBulletTime = 0;
nextBullet = 0;
bullets = [];

// init score
score = 0;
}

function newBalloon() {
// more balloons?
if (nextBalloon < numBalloons) {

// time for next?
if (getTimer() > nextBalloonTime) {

// 50% chance of a new balloon
if (Math.Random() < .5) {

// create new balloon clip
attachMovie("balloon", "balloon"+nextBalloon, nextBalloon);

// choose which side to come from and random speed
if (Math.Random() < .5) {
_root["balloon"+nextBalloon]._x = -30;
dx = int(Math.Random()*3)+3;
} else {
_root["balloon"+nextBalloon]._x = 580;
dx = -int(Math.Random()*3)-3;
}

// choose height of balloon
_root["balloon"+nextBalloon]._y = int(Math.Random()*100)+20;

// choose color of balloon
balloonColor = new Color("balloon"+nextBalloon);
r = int(Math.Random()*5)+1;
if (r == 1) {
balloonColor.setTransform({rb: 255});
} else if (r == 2) {
balloonColor.setTransform({gb: 255});
} else if (r == 3) {
balloonColor.setTransform({bb: 255});
} else if (r == 4) {
balloonColor.setTransform({rb: 255, gb: 255});
} else if (r == 5) {
balloonColor.setTransform({rb: 255, bb: 255});
}

// add balloon to array
balloons.push({clip: "balloon"+nextBalloon, d: dx});

// set things up for next balloon
nextBalloon++;
nextBalloonTime = getTimer() + 2000;
}
}
}

}

function moveBalloons() {
// loop through balloons in array
for(i=balloons.length-1;i>=0;i--) {

// get speed and clip
dx = balloons[i].d;
balloon = _root[balloons[i].clip];

// move balloon
balloon._x += dx;

// balloon exit left
if ((dx < 0) and (balloon._x < -20)) {
balloon.removeMovieClip();
balloons.splice(i,1);

// balloon exit right
} else if ((dx > 0) and (balloon._x > 570)) {
balloon.removeMovieClip();
balloons.splice(i,1);
}
}

// see if all balloons gone
if ((nextBalloon >= numBalloons) and (balloons.length < 1)) {
gotoAndStop("game over");
}

}

function moveFox() {
// move fox to left, no flip
if (Key.isDown(Key.LEFT)) {
dx = -10;
fox._xscale = Math.abs(fox._xscale);

// move fox to right, flip
} else if (Key.isDown(Key.RIGHT)) {
dx = 10;
fox._xscale = -Math.abs(fox._xscale);

// stop moving
} else {
dx = 0;
}

// limit fox movement
fox._x += dx;
if (fox._x < 30) fox._x = 30;
if (fox._x > 520) fox._x = 520;

// move fox
fox._x += dx;

if ((dx == 0) and (fox._currentFrame != 1)) {
// set fox to stand
fox.gotoAndStop(1);
} else if ((dx != 0) and (fox._currentFrame == 1)) {
// set fox to move
fox.gotoAndPlay(2);
}
}

function shootBullet() {
// see if there has been enough time to reload
if (getTimer() > nextBulletTime) {

// create bullet clip
attachMovie("bullet","bullet"+nextBullet,nextBulle t+9999);

// set location
_root["bullet"+nextBullet]._x = fox._x+2;
_root["bullet"+nextBullet]._y = fox._y-55;

// add to array
bullets.push(nextBullet);

//get set for next bullet
nextBullet++;
nextBulletTime = getTimer()+1000;

}

}

function moveBullets() {
// loop through all bullets
for(i=bullets.length-1;i>=0;i--) {

// get clip
bullet = _root["bullet"+bullets[i]];

// move clip
bullet._y -= 10;

// see if it reached top
if (bullet._y < 0) {
bullet.removeMovieClip();
bullets.splice(i,1);

// see if it hit a balloon
} else {
if (checkCollision(bullet)) {
bullet.removeMovieClip();
bullets.splice(i,1);
}


}
}

}

function checkCollision(bullet) {
// loop though all balloons
for(j=balloons.length-1;j>=0;j--) {
balloon = _root[balloons[j].clip];

// see if the bullet is close to the balloon
if (distance(bullet,balloon) < 40) {

// go to "pop" frame of balloon
balloon.gotoAndPlay(2);

// remove balloon from array
balloons.splice(j,1);

// add to score
score += 1;

// return true, since there was a collision
return(true);
}

}

// return false, since there was a collision
return(false);


}

function distance(clip1, clip2) {
// find distance between two movie clips
dx = clip1._x - clip2._x;
dy = clip1._y - clip2._y;
return (Math.sqrt(dx*dx+dy*dy));
}

stop();



Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-12-2008, 08:57 PM
kglad
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

create a maxBullets variable, assign it a number and use an if-statement in shootBullet() to check if nextBullet exceeds maxBullets.
Reply With Quote
  #3 (permalink)  
Old 10-13-2008, 02:35 AM
coolburn
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

First, thanks klad for your help. I did what you said and it seems to work but
I'm having a new
problem with this game. After you have fired all of the bullets and the game
ends, some of the
bullets that have been fired and the balloons that have not been hit, stay on
the screen, even
after you restart the game. How can I fix this problem? Thanks for any help
you can give me. I
have attached the code that I changed, in case it's something that I did.

coolburn

function initGame() {
// init balloon variables
nextBalloonTime = 0;
nextBalloon = 0;
balloons = [];
numBalloons = 40;

// init bullet variables
maxBullets = 40;
nextBulletTime = 0;
nextBullet = 0;
bullets = [];

// init score
score = 0;
}

function newBalloon() {
// more balloons?
if (nextBalloon < numBalloons) {

// time for next?
if (getTimer() > nextBalloonTime) {

// 50% chance of a new balloon
if (Math.Random() < .5) {

// create new balloon clip
attachMovie("balloon", "balloon"+nextBalloon, nextBalloon);

// choose which side to come from and random speed
if (Math.Random() < .5) {
_root["balloon"+nextBalloon]._x = -30;
dx = int(Math.Random()*3)+3;
} else {
_root["balloon"+nextBalloon]._x = 580;
dx = -int(Math.Random()*3)-3;
}

// choose height of balloon
_root["balloon"+nextBalloon]._y = int(Math.Random()*100)+20;

// choose color of balloon
balloonColor = new Color("balloon"+nextBalloon);
r = int(Math.Random()*5)+1;
if (r == 1) {
balloonColor.setTransform({rb: 255});
} else if (r == 2) {
balloonColor.setTransform({gb: 255});
} else if (r == 3) {
balloonColor.setTransform({bb: 255});
} else if (r == 4) {
balloonColor.setTransform({rb: 255, gb: 255});
} else if (r == 5) {
balloonColor.setTransform({rb: 255, bb: 255});
}

// add balloon to array
balloons.push({clip: "balloon"+nextBalloon, d: dx});

// set things up for next balloon
nextBalloon++;
nextBalloonTime = getTimer() + 2000;
}
}
}

}

function moveBalloons() {
// loop through balloons in array
for(i=balloons.length-1;i>=0;i--) {

// get speed and clip
dx = balloons[i].d;
balloon = _root[balloons[i].clip];

// move balloon
balloon._x += dx;

// balloon exit left
if ((dx < 0) and (balloon._x < -20)) {
balloon.removeMovieClip();
balloons.splice(i,1);

// balloon exit right
} else if ((dx > 0) and (balloon._x > 570)) {
balloon.removeMovieClip();
balloons.splice(i,1);
}
}

// see if all balloons gone
if ((nextBalloon >= numBalloons) and (balloons.length < 1)) {
gotoAndStop("game over");
}

}

function moveFox() {
// move fox to left, no flip
if (Key.isDown(Key.LEFT)) {
dx = -10;
fox._xscale = Math.abs(fox._xscale);

// move fox to right, flip
} else if (Key.isDown(Key.RIGHT)) {
dx = 10;
fox._xscale = -Math.abs(fox._xscale);

// stop moving
} else {
dx = 0;
}

// limit fox movement
fox._x += dx;
if (fox._x < 30) fox._x = 30;
if (fox._x > 520) fox._x = 520;

// move fox
fox._x += dx;

if ((dx == 0) and (fox._currentFrame != 1)) {
// set fox to stand
fox.gotoAndStop(1);
} else if ((dx != 0) and (fox._currentFrame == 1)) {
// set fox to move
fox.gotoAndPlay(2);
}
}

function shootBullet() {
// see if there has been enough time to reload
if (getTimer() > nextBulletTime) {

// create bullet clip
attachMovie("bullet","bullet"+nextBullet,nextBulle t+9999);

// set location
_root["bullet"+nextBullet]._x = fox._x+2;
_root["bullet"+nextBullet]._y = fox._y-55;

// add to array
bullets.push(nextBullet);

// check to see if nextBullet exceeds maxBullets
if (nextBullet > maxBullets) {
gotoAndStop ("game over"); }

//get set for next bullet
nextBullet++;
nextBulletTime = getTimer()+1000;

}

}

function moveBullets() {
// loop through all bullets
for(i=bullets.length-1;i>=0;i--) {

// get clip
bullet = _root["bullet"+bullets[i]];

// move clip
bullet._y -= 10;

// see if it reached top
if (bullet._y < 0) {
bullet.removeMovieClip();
bullets.splice(i,1);

// see if it hit a balloon
} else {
if (checkCollision(bullet)) {
bullet.removeMovieClip();
bullets.splice(i,1);
}


}
}

}

function checkCollision(bullet) {
// loop though all balloons
for(j=balloons.length-1;j>=0;j--) {
balloon = _root[balloons[j].clip];

// see if the bullet is close to the balloon
if (distance(bullet,balloon) < 40) {

// go to "pop" frame of balloon
balloon.gotoAndPlay(2);

// remove balloon from array
balloons.splice(j,1);

// add to score
score += 1;

// return true, since there was a collision
return(true);
}

}

// return false, since there was a collision
return(false);


}

function distance(clip1, clip2) {
// find distance between two movie clips
dx = clip1._x - clip2._x;
dy = clip1._y - clip2._y;
return (Math.sqrt(dx*dx+dy*dy));
}

stop();

Reply With Quote


  #4 (permalink)  
Old 10-13-2008, 06:19 AM
kglad
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

to remove those movieclips call the following function:



var tl:MovieClip=this;

function remove BalloonsAndBulletsF(){
for(mc in tl){
if(mc._name.indexOf("balloon")>-1||mc._name.indexOf("bullet")>-1){
tl[mc].removeMovieClip();
}
}
}

Reply With Quote
  #5 (permalink)  
Old 10-13-2008, 05:02 PM
coolburn
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

Kglad, thanks again for your help. I put the code in that you gave me but I get
the following error message when I test my game: Scene=Scene 1, Layer=objects,
Frame=3: Line 225: '(' expected
function remove BalloonsAndBulletsF(){

I thought it might be because of where I added the code that you gave to the
other code. I have attached the code as it is right now. I appreciate any help
you can give me in adding the code you gave me to the other code. I also needed
to find out, how I would make it so that the player could see the number of
bullets that they start with, count down as they fire them. Ex: show 40 before
they fire and the count down as they fire 39,38, etc.. Thanks again for all of
the help that you have given me and any future help you can give me.

function initGame() {
// init balloon variables
nextBalloonTime = 0;
nextBalloon = 0;
balloons = [];
numBalloons = 40;

// init bullet variables
maxBullets = 40;
nextBulletTime = 0;
nextBullet = 0;
bullets = [];

// init score
score = 0;
}

function newBalloon() {
// more balloons?
if (nextBalloon < numBalloons) {

// time for next?
if (getTimer() > nextBalloonTime) {

// 50% chance of a new balloon
if (Math.Random() < .5) {

// create new balloon clip
attachMovie("balloon", "balloon"+nextBalloon, nextBalloon);

// choose which side to come from and random speed
if (Math.Random() < .5) {
_root["balloon"+nextBalloon]._x = -30;
dx = int(Math.Random()*3)+3;
} else {
_root["balloon"+nextBalloon]._x = 580;
dx = -int(Math.Random()*3)-3;
}

// choose height of balloon
_root["balloon"+nextBalloon]._y = int(Math.Random()*100)+20;

// choose color of balloon
balloonColor = new Color("balloon"+nextBalloon);
r = int(Math.Random()*5)+1;
if (r == 1) {
balloonColor.setTransform({rb: 255});
} else if (r == 2) {
balloonColor.setTransform({gb: 255});
} else if (r == 3) {
balloonColor.setTransform({bb: 255});
} else if (r == 4) {
balloonColor.setTransform({rb: 255, gb: 255});
} else if (r == 5) {
balloonColor.setTransform({rb: 255, bb: 255});
}

// add balloon to array
balloons.push({clip: "balloon"+nextBalloon, d: dx});

// set things up for next balloon
nextBalloon++;
nextBalloonTime = getTimer() + 2000;
}
}
}

}

function moveBalloons() {
// loop through balloons in array
for(i=balloons.length-1;i>=0;i--) {

// get speed and clip
dx = balloons[i].d;
balloon = _root[balloons[i].clip];

// move balloon
balloon._x += dx;

// balloon exit left
if ((dx < 0) and (balloon._x < -20)) {
balloon.removeMovieClip();
balloons.splice(i,1);

// balloon exit right
} else if ((dx > 0) and (balloon._x > 570)) {
balloon.removeMovieClip();
balloons.splice(i,1);
}
}

// see if all balloons gone
if ((nextBalloon >= numBalloons) and (balloons.length < 1)) {
gotoAndStop("game over");
}

}

function moveFox() {
// move fox to left, no flip
if (Key.isDown(Key.LEFT)) {
dx = -10;
fox._xscale = Math.abs(fox._xscale);

// move fox to right, flip
} else if (Key.isDown(Key.RIGHT)) {
dx = 10;
fox._xscale = -Math.abs(fox._xscale);

// stop moving
} else {
dx = 0;
}

// limit fox movement
fox._x += dx;
if (fox._x < 30) fox._x = 30;
if (fox._x > 520) fox._x = 520;

// move fox
fox._x += dx;

if ((dx == 0) and (fox._currentFrame != 1)) {
// set fox to stand
fox.gotoAndStop(1);
} else if ((dx != 0) and (fox._currentFrame == 1)) {
// set fox to move
fox.gotoAndPlay(2);
}
}

function shootBullet() {
// see if there has been enough time to reload
if (getTimer() > nextBulletTime) {

// create bullet clip
attachMovie("bullet","bullet"+nextBullet,nextBulle t+9999);

// set location
_root["bullet"+nextBullet]._x = fox._x+2;
_root["bullet"+nextBullet]._y = fox._y-55;

// add to array
bullets.push(nextBullet);

// check to see if nextBullet exceeds maxBullets
if (nextBullet > maxBullets) {
gotoAndStop ("game over"); }

//get set for next bullet
nextBullet++;
nextBulletTime = getTimer()+1000;

}

}

function moveBullets() {
// loop through all bullets
for(i=bullets.length-1;i>=0;i--) {

// get clip
bullet = _root["bullet"+bullets[i]];

// move clip
bullet._y -= 10;

// see if it reached top
if (bullet._y < 0) {
bullet.removeMovieClip();
bullets.splice(i,1);

// see if it hit a balloon
} else {
if (checkCollision(bullet)) {
bullet.removeMovieClip();
bullets.splice(i,1);
}


}
}

}

function checkCollision(bullet) {
// loop though all balloons
for(j=balloons.length-1;j>=0;j--) {
balloon = _root[balloons[j].clip];

// see if the bullet is close to the balloon
if (distance(bullet,balloon) < 40) {

// go to "pop" frame of balloon
balloon.gotoAndPlay(2);

// remove balloon from array
balloons.splice(j,1);

// add to score
score += 1;

// return true, since there was a collision
return(true);
}

}

// return false, since there was a collision
return(false);


}

function distance(clip1, clip2) {
// find distance between two movie clips
dx = clip1._x - clip2._x;
dy = clip1._y - clip2._y;
return (Math.sqrt(dx*dx+dy*dy));
}

var tl:MovieClip=this;

function remove BalloonsAndBulletsF(){
for(mc in tl){
if(mc._name.indexOf("balloon")>-1||mc._name.indexOf("bullet")>-1){
tl[mc].removeMovieClip();
}
}

stop();

Reply With Quote
  #6 (permalink)  
Old 10-13-2008, 05:54 PM
kglad
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

there should be any space between remove and BalloonsAndBulletsF(){

var tl:MovieClip=this;

function removeBalloonsAndBulletsF(){
for(mc in tl){
if(mc._name.indexOf("balloon")>-1||mc._name.indexOf("bullet")>-1){
tl[mc].removeMovieClip();
}
}
}


Reply With Quote


  #7 (permalink)  
Old 10-13-2008, 06:51 PM
coolburn
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

Thanks kglad for helping me again. I changed the code like you said and the
error message went away, but the balloons and bullets still stay on the screen,
even after restarting the game. This problem wasn't happening until I added the
code to limit the bullets. I'm not sure what is going on.

coolburn

Reply With Quote
  #8 (permalink)  
Old 10-13-2008, 07:01 PM
kglad
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

use the attach code option to show your shootBullet() function.
Reply With Quote
  #9 (permalink)  
Old 10-13-2008, 07:32 PM
coolburn
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

Kglad, here is the shootBullet function code.

coolburn

function shootBullet() {
// see if there has been enough time to reload
if (getTimer() > nextBulletTime) {

// create bullet clip
attachMovie("bullet","bullet"+nextBullet,nextBulle t+9999);

// set location
_root["bullet"+nextBullet]._x = fox._x+2;
_root["bullet"+nextBullet]._y = fox._y-55;

// add to array
bullets.push(nextBullet);

// check to see if nextBullet exceeds maxBullets
if (nextBullet > maxBullets) {
gotoAndStop ("game over"); }

//get set for next bullet
nextBullet++;
nextBulletTime = getTimer()+1000;

}

}

Reply With Quote


  #10 (permalink)  
Old 10-13-2008, 07:42 PM
kglad
 
Posts: n/a
Diggs:
Default Re: Limited Ammo

where are you calling removeBalloonsAndBulletsF()?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
All times are GMT. The time now is 04:23 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Inactive Reminders By Mished.co.uk