Page 1 of 2 12 LastLast
Results 1 to 10 of 15
  1. #1
    Join Date
    Dec 2009
    Posts
    10

    Making a sidescrolling platformer in AS3 - Problems

    Hi!

    I've just become a member on this site because of a tip from my teacher. I'm currently creating (programming/designing) a rather simple sidescrolling platformer in Actionscript 3.0 (using Flash CS3.
    I've come up with some problems while programming which, according to my teacher, you should be able to fix quite easily.

    Firstly, take a look at the game so far here: http://rs20l3.rapidshare.com/cgi-bin...98017084774025 (if it has been dld >10 times, tell me and I'll reupload).

    Now, apart from the apparent ugly design of the game, which I'm going to fix after all of the programming's done, there are some problems.
    The main problem is that if you press right/left, and keep it pressed for a while, the main-guys walking animation freezes/lags, and I can't figure out why.

    Here's the code for his eventlisteners:

    function pressKey(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.LEFT) {
    left = true;
    gubbe.scaleX = -1;
    if (!inAir && !down) {
    gubbe.gotoAndStop(2);
    }
    }

    if(event.keyCode == Keyboard.SHIFT) {
    if(gun1 == true) {
    gun1 = false;
    } else if (gun1 == false) {
    gun1 = true;
    }
    }

    if (event.keyCode == Keyboard.RIGHT) {
    right = true;
    gubbe.scaleX = 1;
    if (!inAir && !down) {
    gubbe.gotoAndStop(2);
    }

    } if (event.keyCode == Keyboard.UP) {
    upp = true;
    gubbe.gotoAndStop(9);

    } if (event.keyCode == Keyboard.DOWN) {
    down = true;
    gubbe.gotoAndStop(10);
    } if(event.keyCode == 32 && shootAllow){

    if (gubbe.scaleX == 1 && gun1) {
    shootAllow = false;
    var newHBullet:HBullet = new HBullet();
    newHBullet.x = gubbe.x + 48;
    newHBullet.y = gubbe.y - 16;
    gubbe.scaleX = 1;
    svinbulletContainer.addChild(newHBullet);
    } if (gubbe.scaleX == -1 && gun1) {
    shootAllow = false;
    var newVBullet:VBullet = new VBullet();
    newVBullet.x = gubbe.x - 48;
    newVBullet.y = gubbe.y - 16;
    svinbulletContainer.addChild(newVBullet);
    gubbe.scaleX = -1;
    } if (gubbe.scaleX == 1 && !gun1) {
    shootAllow = false;
    var newHBullet2:hogerkula = new hogerkula();
    newHBullet2.x = gubbe.x + 48;
    newHBullet2.y = gubbe.y - 16;
    gubbe.scaleX = 1;
    manbulletContainer.addChild(newHBullet2);
    }
    if (gubbe.scaleX == -1 && !gun1) {
    shootAllow = false;
    var newVBullet2:VBullet2 = new VBullet2();
    newVBullet2.x = gubbe.x - 48;
    newVBullet2.y = gubbe.y - 16;
    gubbe.scaleX = -1;
    manbulletContainer.addChild(newVBullet2);
    }
    }
    }


    (gubbe = main-guy, I'm swedish ^^)
    This is for when a key is pressed, the long part with the space-button is for when the character shoots, I guess this could be shortened quite a bit but I don't know how.

    function releaseKey(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.LEFT) {
    if(!inAir) {
    gubbe.gotoAndStop(1);
    }
    left = false;
    gubbe.scaleX = -1;

    } if (event.keyCode == Keyboard.RIGHT) {
    if(!inAir) {
    gubbe.gotoAndStop(1);
    }
    right = false;
    gubbe.scaleX = 1;

    } if (event.keyCode == Keyboard.UP) {
    upp = false;

    }
    if (event.keyCode == Keyboard.DOWN) {
    down = false;
    gubbe.gotoAndStop(1);
    }
    }


    Now, this code is pretty straight forward, I don't think I can make it much better.

    function enterFrame(event:Event):void {


    if(inAir = true) {
    gubbe.y += velocity;
    velocity += gravity;
    (all of these velocity/gravity etc are declared variables)

    }
    if(gubbe.y > 400) {
    gameOver = true;
    gotoAndPlay(2);
    }
    if (gubbe.hitTestObject(floor1)&& (gubbe.y+50) < floor1.y) {
    if(!left && !right && !down) {
    gubbe.gotoAndStop(1);
    }
    velocity = 0;
    inAir = false;

    }
    if (left && !down) {
    floor1.x += speed;
    Bg.x += speed;
    }
    if (right && !down) {
    floor1.x -= speed;
    Bg.x -= speed;
    }
    if(upp && !inAir) {
    velocity = -10;
    gubbe.gotoAndStop(9);
    inAir = true;
    }
    if(cTime < cLimit){
    cTime ++;
    } else {
    shootAllow = true;
    cTime = 0;
    }

    if (totenem < totenemLimit) {
    if(enemyTime < enemyLimit){
    enemyTime ++;
    } else {
    var newEnemy = new enemy();
    newEnemy.x = -750;
    newEnemy.y = 320;
    addChild(newEnemy);
    enemyTime = 0;
    totenem++;
    }
    if(enemy2Time < enemy2Limit){
    enemy2Time ++;
    } else {
    var newEnemy2 = new enemy2();
    newEnemy2.x = 1250;
    newEnemy2.y = 320;
    addChild(newEnemy2);
    enemy2Time = 0;
    totenem++;
    }
    }

    }


    The last couple of lines just describe how often you can shoot and how often enemies should spawn (I have 2 different kinds of enemies).

    So to conclude, I would like some help with fixing the bug with the walking-cycle of the main-guy freezing, and also, if you have ways of improving the code or making it simpler, feel free to tell me.

    God, that's a lot of text, big thanks to anyone who are able to help!

  2. #2
    Join Date
    Mar 2003
    Location
    Brisbane Australia
    Posts
    1,429
    link to file does not work.
    Why not .zip it and attach it?

  3. #3
    Join Date
    Dec 2009
    Posts
    10
    I can't seem to get it to upload it correctly, it just says that the upload failed. However, if you go into the site http://www.megafast.info/ and enter IS3ZDYOF into the generator, it should work (works for me at least).
    I have included both my .as and .fla files (ignore the .swf) in there, so it is easier to get a quick overview of my code than when i just write it in the forum.

    I have pretty much fixed the cycle freezing now, but I have some other quite more severe problems now. Firstly when you die, it starts complaining about Cannot access a property or method of a null object reference.
    at enemy/::eFrame()
    . I do not really understand what this is.
    Secondly, as you might see, I have tried to make the character "fly" away when he is hit by an enemy, but it does not work very well, the enemy seem to stick to him and he does not "fly" away.
    I have also managed to make sort of a lifesystem and a powerbar, but I can't figure out how to make the character invisible for about 1-2 seconds directly after he is hit.

    Also, I feel like my code is very "nooby" and long in comparison with what it does, so some tips on making it more effective would be more than welcome as well.

    I now see that it seems like I am trying to get you (or someone else on this site, but it seems like you are the master of AS3 here ^^) to make the game for me, but I wish you could give me some helpful tips.

    Thanks a bunch in advance =)

  4. #4
    Join Date
    Mar 2003
    Location
    Brisbane Australia
    Posts
    1,429
    Sorry, forgot to look at it today and its 11pm now. I will take a look tomorrow.

  5. #5
    Join Date
    Dec 2009
    Posts
    10
    No worries

  6. #6
    Join Date
    Mar 2003
    Location
    Brisbane Australia
    Posts
    1,429
    remove the enterframe event listener before going to frame 2

    add
    Code:
    stage.removeEventListener(Event.ENTER_FRAME, flyttaGubbe);
    around line 160 frame 1 before the gotoAndPlay(2)

    for some reason the flyttaGubbe still gets called once more so ad a try catch like this on around line 166 frame 1

    Code:
    try{
    	if (gubbe.hitTestObject(floor1)&& (gubbe.y+50) < floor1.y) {
    		if(!vänster && !höger && !ner) {
    			gubbe.gotoAndStop(1);
    		}
    		velocity = 0;
    		iLuften = false;
    	
    	}
    }catch(e:Error){}
    The code is not indented properly and variable and function names are in another language making it very hard for me to read. I think you should really go through and indent your code properly, it will make it a lot easier to read and debug.

    Also, try switch/case statements instead of a ling list of if statements

  7. #7
    Join Date
    Dec 2009
    Posts
    10
    Okay, I've now changed all variables and functions to english names (except for "gubbe", since that obviously is the main char). I've put the removeEventListener where you told, and that now works fine

    I've also changed the if-statements in nedTryckt (pressKey)/släppt (releaseKey)-functions into switch-statements, and indented all of my code properly in both the main timeline and all of the .as-files.

    I don't see any difference when using the try-catch, what is it supposed to do (have tried to google but I don't fully understand)?

    I will post the new improved code tomorrow, but in the meanwhile I've come up with another problem. I tried to make the code shorter by just having 2 kinds of bullets and .as-files, and in their code I've changed

    Code:
    this.x +/-= speed;
    into

    Code:
    if (_root.gubbe.scaleX = 1) {
    this.x += speed;
    }
    if (_root.gubbe.scaleX = -1) {
    this.x -= speed;
    }
    But with this code, the bullet starts going the other way if you turn "gubbe" the other way after you shoot. Is there any way to make the bullet go all the way to either touch an enemy or go to far and be removed without having to make 4 different bullets and 4 different .as-files?

    Thanks a lot for your help this far

  8. #8
    Join Date
    Mar 2003
    Location
    Brisbane Australia
    Posts
    1,429
    Code:
    if (_root.gubbe.scaleX = 1) {
    this.x += speed;
    }
    if (_root.gubbe.scaleX = -1) {
    this.x -= speed;
    }
    should be

    Code:
    if (_root.gubbe.scaleX == 1) {
    this.x += speed;
    }
    if (_root.gubbe.scaleX == -1) {
    this.x -= speed;
    }
    in the first one the if (_root.gubbe.scaleX = 1) is assigning 1 to _root.gubbe.scaleX and will always be true.

  9. #9
    Join Date
    Dec 2009
    Posts
    10
    Oh, yes, that was a mistake I did when I wrote my post rather than in my code, my bad, I already had == in my code.

    It seems like i still can't upload my files to 3dbuzz, so here are my questions:

    1. Is there any way to make the bullet go left when the player is facing left and same for right, other than making two movieclips and their .as-files? I've tried the code where it checks for the scaleX of the player, but in that case the bullet starts going the other way if the player turns around when the bullet is still on screen.

    Here is the new code for when i shoot in the main timeline:
    Code:
    case Keyboard.SPACE:
    		if (shootAllow) {
    			shootAllow = false;
    			if (gun1) {
    				var newBullet:Bullet = new Bullet();
    				if (gubbe.scaleX == 1) {
    					goRight = true;
    					newBullet.x = gubbe.x + 48;
    				} else {
    					goRight = false;
    					newBullet.x = gubbe.x - 48;
    				}
    				newBullet.y = gubbe.y - 16;
    				swinebulletContainer.addChild(newBullet);
    			}
    			if (!gun1) {
    				var newBullet2:Bullet2 = new Bullet2();
    				if (gubbe.scaleX == 1) {
    					goRight2 = true;
    					newBullet2.x = gubbe.x + 48;
    				} else {
    					newBullet2.x = gubbe.x - 48;
    					goRight2 = false;
    				}
    				newBullet2.y = gubbe.y - 16;
    				manbulletContainer.addChild(newBullet2);
    			}
    		}
    		break;
    And here is the code in the bullets (same for both):
    Code:
    private function eFrame(event:Event):void{
    			if(_root.gameOver){
    				removeEventListener(Event.ENTER_FRAME, eFrame);
    				this.parent.removeChild(this);
    			}
    			if (_root.goRight) {
    				x += speed;
    				if(this.x > 800){
    					removeEventListener(Event.ENTER_FRAME, eFrame);
    					this.parent.removeChild(this);
    				}
    			}
    			if (!_root.goRight) {
    				x -= speed;
    				this.scaleX = -1;
    				if (this.x < -300) {
    					removeEventListener(Event.ENTER_FRAME, eFrame);
    					this.parent.removeChild(this);
    				}
    			}
    		}
    2. Nothing happens when I add the try catch, I've read about it but can't figure out what is supposed to happen.

  10. #10
    Join Date
    Dec 2009
    Posts
    10
    Hi!

    Haven't been in for a while but it seems like none has answered anyways. I have managed to fix the bug. Instead of using gotoandPlay(2); when calling the walking animation on keypress, i just wrote play(); Worked like a charm

    Now i would like to write the whole game in one, big .as-file instead of multiple .as-files and a lot of code in the frame. Anyone know where to start with this? Not that good with making .as-files.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •