Help with flash/actioscript/game...?

Started by vict0r, Thu 12/04/2007 10:21:56

Previous topic - Next topic

vict0r

Well, it seems like I've taken on too much to handle.

I'm working on a project for school where I'm supposed to, on my own, learn actionscript and create a flash game engine and a game.
Soo I was very enthusiastic to begin with, and I've come quite a distance from where I started. I've used tutorials and learnt quite alot of actionscript compared with what I knew before, but it has all come to an end it seems.

This might sound alittle weird, but I'm having extreme problems learning anything more when it comes to this lately. I just can't comprehend it anymore. Maybe it's that I've lost interest for this now, but I really need to get this project done. :-\

Now, don't get me wrong as I'm not asking anyone to do any work for me. I just need to know how I do certain things to the script that I already have.

The .fla here..

So what I need to do is to add a way to shoot and add an animation to this. I've tried several tutorials on how to, but I can't seem to make it work. As it is now an orb appears if CTRL is pressed.
I need to add simple enemies and a way to make them disappear when shot.
And I also need to make an exit thingy which will take the player to the next level.

Again, I'm not asking people to do this for me! I just need help so I won't fail this project :-\


- vict0r

EDIT: On second thought, this should maybe be moved to CL...

fred

hey vict0r, I had a look at your fla file, and it looks fine, although for games you usually want to do all scripts on the main timeline instead of on the individual movieclips. It's easier to find the code that way, especially if the game gets complex with many movieclips.
Also, if you have arrays of lasershots and enemies and animations that need to update in sync, you usually have just a single main-style function on the timeline, that then calls all other update functions for movieclips  on stage.

Here's a laser-shooting script that'll fit right into your movie (in my test of it at least):
Code: ags

if (Key.isDown(Key.CONTROL)) {
   laserCounter++;
   var temp:MovieClip = _root.createEmptyMovieClip("laser"+laserCounter,_root.getNextHighestDepth());
   var temp2:MovieClip = temp.attachMovie("laser", "laserContained"+laserCounter,_root.getNextHighestDepth(),     {_x:_root.hero._x,_y:_root.hero._y-30,left:_root.hero._xscale < 0 ? true : false,speed:15});
   temp2.onEnterFrame = function(){
	  //trace ("updating shot"); 
	  if (this.left){
	  	  this._x-= this.speed;
	  } else {
		  this._x+= this.speed;
 	  }
	  if (this._x < 0 || this._x > 550){
		  //shot flew offscreen, so remove it
		  this.removeMovieClip();
	  }


                  //ideas for hitTest scripts
	  //do hittest if there's just one enemy on screen
 	  else if (this.hitTest(_root.enemy)){
		  trace ("enemy hit");
		  //take damage
		  //_root.enemy.hp--;
		  //this.removeMovieClip();
	  }
	  //do hittest with all enemies in array of enemy movieclips
	  //for (var j in _root.enemies){
	  //	if (this.hitTest(_root.enemies[j])){
	  //		trace ("laser hit an enemy");
	  //	}
	  //}
	
 };


For it to work, you must also right-click the "laser" movieclip in the library, select properties, check the "export for actionscript" checkbox and type "laser" in the identifier field. This allows you to attach it directly from the library by that name, instead of duplicating it (where it has to be somewhere on stage already).

You said you didn't want someone to do it for you, so I'm not going to do the rest. But the script is basic for a lot of the things you want to do, where it involves adding or removing new movieclips dynamically and giving them custom update functions.

Hope it helps.

vict0r

#2
Thanks alot! That's alot of help! :)

I'll try to add that script there. But where am I supposed to put it? In the MC, or in the timeline?
I've also managed to add a simple way to get to the next level.

EDIT: I tried to add the code, but I get an error code I'm not fully able to decode: **Error** Symbol=laser, layer=Layer 1, frame=1:Line 1: Statement block must be terminated by '}'
     if (Key.isDown(Key.CONTROL)) {

**Error** Symbol=laser, layer=Layer 1, frame=1:Line 33: Syntax error.
      };


Again, thanks for helping!


Double edit: I'm using flash 8 if that has any relevance!

fred

You can put the script in the hero movieclip timeline where you have most of the script already. It can replace your previous

if (Key.isDown(Key.CONTROL)) {
//do stuff
}

part.

I missed a closing bracket } when I pasted the script. Just add one at the end, and it should work. 

vict0r

Yees! Too great! :D

It's abit rapid tho, but I won't complain! :) I'll try to take that part from now.

I've got another question tho.. If you try the "game", you'll notice that if you jump around alittle, you'll sink down into the platforms. Not too much, but it's quite annoying :-\ Know why this is?

fred

#5
Ok, glad if you made it work.

Hitdetection with platforms may happen before or after the update of hero's position, so sometimes it will only detect the hit when the hero has already been moved down, so he'll land 'sunken' into the platform. That's one of the problems with synchronizing and having multiple onEnterFrame functions - flash isn't told which onEnterFrame to update first on each frame.

You can fix it by either writing separate functions for everything and then having just one onEnterFrame in the movie, that call the other functions each frame in the exact order you want things to happen. That's probably the better way of doing it, especially if you plan to extend the game a lot. If you don't want to rework all scripts like that, you can make an extra check in your script where you have the

if (_root.platforms.hitTest(_x,_y,true)){
//add following line to script as is
while (_root.platforms.hitTest(_x,_y,true)) _y--;
}

It's a hack and not very optimal, but just tells flash that if there's a collision between hero and platform, the hero should be moved slightly upwards, until there's no longer any collision. In that way you'll always have the hero slightly above the platform he lands on. It's fast enough you wont notice the gradual upheaval that takes place. It'll just seem the hero landed a bit higher. Pixel-perfect adjustments (if you want the character to touch the platform just slightly) can be made by adding 1 or 2 to the hitTest _y parameters, or by moving the registration point of the hero MovieClip up or down slightly.

Have fun :)

EDIT:

I was just rereading my post, and while the solution works, the explanation I gave is wrong in this case. You don't have enough simultaneous onEnterFrame functions for flash to mess up, instead the hero sinks into the platform because he's falling with a speed of more than one pixel per frame. So the hit-detection may happen when he's already fallen into the platform. But as said, the above fix will work regardless.

SMF spam blocked by CleanTalk