We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Ironing out problems with simple game
Page Index Toggle Pages: 1
Ironing out problems with simple game (Read 458 times)
Ironing out problems with simple game
Mar 28th, 2008, 1:51am
 
Hey guys, sorry if this is in the wrong section, wasn't really sure.
Some friends and I are trying to make a simple side scroller, hopefully in the end you'll be able to jump, shoot, morph into other characters, upgrade attributes, that kind of thing.  There will also have to be some basic form of enemy ai involved too.
However we'll deal with those problems as we see fit, for right now we're a little stuck on the jumping.  In this early build used for testing stuff the guy (dot) can jump up and technically can jump left or right but it's very awkward.  Ideally we want to be able to have the character be able to run and then press the jump button and be able to move in the direction.  If you could play what there is of the game you'll immediately see the issue, but you can't attach files I guess so you'll just have to base it off of the raw code.  Any tips, suggestions, or code would be greatly appreciated.



int $x = 0;
int $jump = 0;
int $dx = 30;
int $dy = 61;
PImage img;
PImage img1;
PImage img2;

void setup(){
 size (100, 100);
 img = loadImage ("test1.jpg");//level one
 img1 = loadImage ("dots.jpg");//dot or character
 img2 = loadImage ("test2.jpg");//level 2
}

void draw() {
image (img, $x, 0);
image (img1, $dx, $dy);
 
 if ((keyPressed == true) && (key == 'd')){//move right and falling
   $x --;
   $dy ++;
   
 }else if ((keyPressed == true) && (key == 'a')){//move left and falling
   $x ++;
  $dy ++;
 
 }else if ((keyPressed == true) && (key == 'w')){//jump
   if ($jump <= 3){
     $dy = $dy - 2;
     $jump ++;
     if ($jump > 5){
       $dy = $dy + 2;
     }
   }
   
 }else if ((keyPressed == true) && (key == 's')){//move down does do anything
   
   $dy ++;
 
 }else
 $dy ++;
 $jump = 0;
 image (img1, $dx, $dy);
 if ($dy >= 62){//floor boundary
   $dy = 61;
 }  
 if ($dy <= 0){//top boundary
   $dy = 1;
 }
if ($x>=1){//left side boundary
  $x=0;
}
if($x <= -988){//to go to new level
  img = img2;
  $dy=61;
  $dx=30;
  $x=0;
}
if (($x <= -132)&&($x >= -230)&&($dy >= 35)){//top of box 1
 $dy = 34;
}
if (($x == -131)&&($dy >= 35)){//left side box 1  
  $x ++;
}
if (($x == -231)&&($dy >= 35)){//right side box 1
  $x --;
}
if (($x <= -268)&&($x >= -366)&&($dy >= 49)){//top of box 2
 $dy = 48;
}
if (($x == -267)&&($dy >= 55)){//left side box 2  
  $x ++;
}
if (($x == -367)&&($dy >= 55)){//right side box 2
  $x --;
}
 
}



Re: Ironing out problems with simple game
Reply #1 - Mar 28th, 2008, 3:02am
 
If you can detail the problem more precisely it would help a lot. Like, what exactly are you having difficulties with. The more you can pinpoint the problem for us, the better people can help you.

Keep up the good work. Smiley
Re: Ironing out problems with simple game
Reply #2 - Mar 28th, 2008, 5:01pm
 
Thanks, okay it's a little tricky to describe and show at the same time but I'll try again.
Basically we want to be able to press and or hold one key to make the character run in either direction and while running if another key is pressed the character should be able to jump in the direction the character is currently moving.  As it is now this doesn't work.  You can run, and when standing still you can jump but we don't know how to code it so there is not only a predetermined jump height (like you press 'w' and the character jumps so many pixels high), but also a way to jump while running.  As it stands when you press 'w' the character moves up the screen and you can literally float them at the top, it's not like a normal game where jumping is a singular action where a single key press allows for a single jump.  Hope this makes a little more sense.

Here's the part of the code where jumping is determined:

  }else if ((keyPressed == true) && (key == 'w')){//jump
   if ($jump <= 3){
$dy = $dy - 2;
$jump ++;
if ($jump > 5){
  $dy = $dy + 2;
}
   }
Thanks again
Re: Ironing out problems with simple game
Reply #3 - Mar 28th, 2008, 6:07pm
 
Hey i don't know if it'll work out the way you like but why not just use an "if" instead of if else. Also you might like to change it such that a jump sets the state of the sprite to a "jump" state and then in the draw routine, you determine whether the sprite is going up or down and how fast.
Re: Ironing out problems with simple game
Reply #4 - Mar 31st, 2008, 2:05am
 
Thanks disablek, the if verses if else didn't do the job unfortunately but right now we're working on figuring out objects and that sort of thing which will work to solve other issues we have hopefully.
Re: Ironing out problems with simple game
Reply #5 - Mar 31st, 2008, 11:41am
 
Maybe you should try to overload the keypressed()function, as opposed to the keyPressed boolean in the draw function, and do the processing there, because from the other examples i've encountered for multiple keypresses, there's a handler for the KeyEvent, so multiple events can happen at the same time. Probably the keypressed() function is already setup this way anyway, so it'll just work.

Page Index Toggle Pages: 1