![]() |
![]() |
||||||
|
|||||||
| Tags: |
![]() |
|
|||
|
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(); |
| Sponsored Links |
|
|||
|
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(); |
|
|||
|
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(); } } } |
|
|||
|
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(); |
|
|||
|
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(); } } } |
|
|||
|
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 |
|
|||
|
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; } } |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise