Shooting in a game-megaman style

Ok I am creating a 2d game similar to the original mega mans and mario for nintendo. The character moves back and forth using the left and right arrow keys on the screen and has a walk animation and all. The next step is to get him to shoot his laser gun. I want this to be done using the space bar key. What I have right now is identical to Kirupa’s movement using X and Y values tutorial but it only moves one direction of course. How can I make it so that it will continue to move automatically after pressing space instead of having to hit the space bar each time I want it to move 10 units over etc.?\r\rThis is probably a simple question but I am new to flash. Most of my experience is in VB and C++ :slight_smile:

this is pretty simple to do, however, i’m a moron, and I can explain it. \r\r\rthe code on your button should look like this::\r<font color=blue>\ron (keyPress “<Right>”) {\r _root.person._x = _root.person._x+5;\r}\ron (keyPress “<Left>”) {\r _root.person._x = _root.person._x-5;\r}\ron (keyPress “<Space>”) {\r_root.bullet.gotoAndPlay(2)\r}</font>\r\rperson is the person, and bullet is the thing you are going to be shooting\r\rBullet is the name of the MC. Bullet has to have two frames, with a “stop()” action in each frame. however, the frist frame must be empty. and the second frame will hold what you actually want to move across the screen. (the object in the second frame must also be a movie clip)\r\ron the main stage, select the movieclip “bullet” which should just be a small white circle because it has nothing in the first frame. and put this code on it.\r<font color=blue>\ronClipEvent(enterFrame){\r_x = _root.person._x\r_y = _root.person._y\r}</font>\r\rthen on the MovieClip in the second frame of “bullet” put this code\r<font color=blue>\ronClipEvent (load){\rxVel = 6\r}\ronClipEvent (enterFrame){\r_x += xVel\r}</font>\r\rthis is what it looks like::\r\r<embed src=“http://www.angelfire.com/dc/vidgame/game/Movie2.swf” height=200 width=200></embed>\r\rif you have any other questions post.

jeremydecker.s5.com/shots_fired.swf\r[url=“http://jeremydecker.s5.com/shots_fired.fla”]jeremydecker.s5.com/shots_fired.fla\r\rhere’s a sample file.\r:) \rjeremy

Very nice Jubby. Great design. I really don’t understand how you could mke fun of my dragoon…\rBy the way, you should really learn to use the duplicate movie clip function.\r\rpom 0]

you like that design? i like it too. I was too dam lazy to draw anything else. yeah i keep forgetting to use duplicateMovieClip. I have a basic understand, but i should work on it a little more.\r\rVery nice Sinf, everything is much easier if you know all the actionscript. Whats this line?\r\rthis.attachMovie(“mcShot”,“mcShot”+nCount++,nCount);\r\rwhere do you define nCount? why do you have two + after the first nCount? and where is mcShot, i coudln’t find it on ?any stage. So how do you attach it?

this.attachMovie(“mcShot”,“mcShot”+nCount++,nCount);\r\r’nCount++’ means to add 1 to the variable ‘nCount’. since it’s undeclared it has no value. adding 1 to it basically declares it as 1. from that point on it just increments.\rtry this:\rtrace(typeof(nCount++));\rtrace(typeof(nCount));\rit will return ‘undefined’ in the first line and ‘number’ in the second.\rnow, to use attach movie you just go into the library and right click on the movie clip and select ‘linkage’ in the menu that pops up select ‘export this symbol’ and give it a name. that name is just like to put an instance on the stage and gave it a name, except you don’t have to worry about an extra movie clip on the stage!\r:) \rjeremy

thanks for the explanation sinf

Thanks a ton for the help to both of you. He now moves and shoots lasers that come from the gun and move on their own. I tried using that duplicate movie clip but I can’t get it to work for the life of me. In something like VB or C++ I’d just create an array for the object that is the laser and make a new instance of the laser each time, but this doesn’t seem to be the case with flash. I went to linkage under the laser movie clip and named it “laser” but where would I put the attach movie clip line? My code is generally similar to Jubba’s in structure with several things added in. Thanks again you guys are great.

you can create an array that stores references to all the ‘laser’ objects. but that’s another topic.\ranyways, you would put your attachmovie code wherever you have the code for your controls. just make sure that you attach the movie to the _root and not a clip that moves on it’s own.\rso, instead of…\rthis.attachMovie(“mcShot”,“mcShot”+nCount++,nCount);\ryou would use…\r_root.attachMovie(“mcShot”,“mcShot”+nCount++,nCount);\r\r:) \rjeremy

ok I got that all working. He moves, walks, shoots and now there is an enemy that comes at him. My next problem lies in the hit test function so the enemy can be shot. I read just about every hit test tutorial on the web and I am having trouble applying it to my game. I completely reworked my code today so it more resemble’s Sinf’s and can fire multiple shots. I put the hit test on the MC for the enemy and need to input the name of the instance of the laser to check to see if it hit it. My laser MC is called mcLaser right now. I have the line of code…\r\r_root.attachMovie(“mcLaser”,“mcLaser”+nCount++,nCount);\r\rfrom Sinf. this looks like it refers to the movie clip, then creates a new instance of it with a number after the name, incrementing by one. My problem is when I do the hit test it looks like…\r\rif (_root.badguy, hitTest(_root.mcLaser+nCount++)) {\r z=_root.bh+500;\r _root.badguy._x=z;\r}\r\rThis code is in the enemy movie clip. bh is the x coord of badguy, which is the instance of the movie clip containing the enemy. So this should blow the enemy backward 500 units, but it doesn’t. Here is my .fla because its getting a little too complex to explain now.\r\rmembers.cox.net/jaedan/flash/game.fla\r[url=“http://members.cox.net/jaedan/flash/game.swf”]members.cox.net/jaedan/flash/game.swf\r\r\rP.S. Good idea to use counterstrike sounds :stuck_out_tongue:

here’s the problem with that code…\rwhere it says ‘nCount++’ that means that you are incrementing ‘nCount’ again and you only want to do that when you attach the movie.\rif there is only one bullet on the screen at a time, do this:\rif(_root[“mcLaser”+nCount].hitTest(_root.badguy)){\r// fun things happen here…\r}\r\rif you have many bullets there is a different way to handle that - but it looks like just the one so i won’t go into that can of worms just yet!\r:) \rjeremy

I see what you mean with the nCount++ thing, that was an oversight by me, but I did replace my hit test code with yours and played with it and it still doesn’t work :frowning:

ok, here’s a sample game. something i started and didn’t finish, but it has all the components of what you are trying to do. the shots go up instead of left/right.\rjeremydecker.s5.com/Cheese_Game.swf\r[url=“http://jeremydecker.s5.com/Cheese_Game.fla”]jeremydecker.s5.com/Cheese_Game.fla\r\r:) \rjeremy