How to make spikes

Hey, i am new user in processing and java.
Im creating my first game, and i need help of making randomly spawning objects that kills you if you hit it.
I want those objects spawn to right and going to left (player is in left and can only jump). Also if someone want i need also help with moving ground and that will make illusion to player that is walking and there is coming objects what u need to evade with jumping. I have made now just ground line and jumping. Tonni is character and tausta is background.

Thanks!
Also i hope someone wanna help me with this problem because i dont know how to make it like i want.

NEW PROBLEM IN COMMENTS, IF YOU KNOW HOW TO FIX IT OR YOU CAN MAKE WORKING VERSION OF THAT JUMPING! THANKS ALOT.

1 Like
1 Like

I dont know how to insert flappy bird pipe code to mine code because type is different and this is my first game what i make so i need help with that.

1 Like

https://discourse.processing.org/search?context=topic&context_id=17935&q=Infinite%20runner%20&skip_context=true

1 Like
  if (keyPressed) {
    PVector up = new PVector(0, -5);
    boolean jumping = false;
    ha.applyForce(up);

how to make when you press space you will only jump once. I know i need to make it with booleans but im new with theses so i need help :smiley:

1 Like

Instead of using keyPressed as a variable, you can use keyPressed() as a function

The brackets () show the difference

Okay thanks! I will test now.

I met problem, after 30mins of try i dont know anymore how to make this.
If you have time i can send full code to you if you wanna fix my jump problem. I have only 1 error (fixed others). I think this is just easy to fix but i dont know how to do it :smile:. Say if you have time i will send this code to you. (its bit of messy because tested other movement options.

Can you post here?

I am on a journey

Okay. And i think its easier that i explain where is my jump codes ect. so u dont need to read all the code. So the error comes from void draw.

    if(jump == true) {
        jump();
    }

I have my void jump(); inside class hahmo (means player).

 void jump() {
        velocity = velocity + 0.5;
        if(ypos + velocity > (height/2)) {
            ypos = height/2;
            jump = false;
            velocity = velocityconst;
        } else {
            ypos += velocity;
        }
    }

And the keypress thing is below anything not in any class and looking like this.

void keyPressed() {
  if (key == ' ' || key == ' ') {
        jump = true;
    } 
}

If you dont get anything from my explain of the code. I can send full code here, but hope you get where is problem.

1 Like

Please explain what happens falsely and what you want to happen

Okay.
When you press space i want it jumps only once…

I tried to make it jump once but now it doesnt jump, even float.
Just giving me error.

The function jump() does not exist.

here is picture too: https://gyazo.com/15f9259fbeb187f9e91eb32ae5d582fa

1 Like

Maybe you must say ha.jump(); when jump() function is inside of the class

Your posts are very good, in terms of how you format your code by the way.

Welcome to the forum! Good to have you here.

1 Like

That fixed my error but jumping doesnt work :confused:

Not sure about this but when you jump the ypos must be decreased, because 0 is upper left corner and the lower screen border is height (for example 800)

So the velocity should be negative.

The way I used to do it was to give a very high negative value to velocity and add it throughout to ypos. And also reduce velocity by gravity until it becomes 0 again.

1 Like

And thanks! This is my first post to this forum :slight_smile:

Hmm… I dont know if problem is in my jump system or in the velocity thing.
If you want i can send full code if you wanna test it. I think you will know faster where is problem.

Hope we found a problem together. Velocity change didnt helped, feels like it doesnt respond to key press but if that is problem it would give error… Hmm

Hahmo ha;
int wid = 1200;
int rez = 800;
int score = 0;
PImage hahmo;
PImage este;
boolean jump;
float velocity;
float velocityconst;
float baseY, px, py, vy, ay, w, h;
float ypos, xpos;
PVector gravity = new PVector(0, 0.5);
ArrayList<Este> Esteet = new ArrayList<Este>();

void setup() {
  size(1200, 800);
  ha = new Hahmo(color(255,0,0),0,height/2,10);
  velocityconst = -5.0;
  velocity =velocityconst;
  Esteet.add(new Este());
  //hahmo = loadImage("hahmo.png");
  //este = loadImage("este.png");
}


void draw() {
  background(0);

    if(jump == true) {
        ha.jump();
    }
    
  if (frameCount % 180 == 0) {
    Esteet.add(new Este());
  }

  ha.update();
  ha.show();
boolean safe = true;

  for (int i = Esteet.size() - 1; i >= 0; i--) {
    Este p = Esteet.get(i);
    p.update();
    
    if (p.hits(ha)) {
      p.show(true);
      safe = false;
    } else {
      p.show(false);
    }

    if (p.x < -p.w) {
      Esteet.remove(i);
    }
  }
  
  if (safe) {
    score++; 
  } else {
    score -= 50;
  }
  
  fill(255, 0, 255);
  textSize(64);
  text(score, width/2, 50);
  score = constrain(score, 0, score);
  vy+=ay;
  py+=vy;
  rect(px, py-h, w, h);
}

class Hahmo {
  color c;
  PVector pos;
  PVector vel;
  PVector acc;
  float r = 16;
  float xpos;
  float ypos;
  float xspeed;

    Hahmo(color clr, float xPos, float yPos, float xSpeed) {
        c = clr;
        pos = new PVector(50, height/1);
        vel = new PVector(0, 0);
        acc = new PVector();
        xpos = xPos;
        ypos = yPos;
        xspeed = xSpeed;

}
  void applyForce(PVector force) {
    acc.add(force);
  }

 void jump() {
        velocity = velocity + 0.5;
        if(ypos + velocity > (height/2)) {
            ypos = height/2;
            jump = false;
            velocity = velocityconst;
        } else {
            ypos -= velocity;
        }
    }
  void update() {
    applyForce(gravity);
    pos.add(vel);
    vel.add(acc);
    vel.limit(4);
    acc.mult(0);

    if (pos.y > height) {
      pos.y = height;
      vel.mult(0);
    }
  }

  void show() {
    stroke(255);
    fill(255);
    ellipse(pos.x, pos.y, r*2, r*2);
  }
}

class Este {
  float x;
  float bottom;
  float w = 40;

  Este() {
    x = wid + w;
    bottom = random(30, height/15);
  }

  boolean hits(Hahmo ha) {
    if ((ha.pos.x > x) && (ha.pos.x < (x + w))) {
      if ((ha.pos.y > (height - bottom - ha.r))) {
        return true;
      }
    }
    return false;
  }

  void update() {
    x -= 3;
  }

  void show(boolean hit) {
    stroke(255);
    
    if (hit) {      
      fill(255, 0, 0);
    } else {
      fill(255);
    }
    

    rect(x, height - bottom, w, bottom);
  }
}
void keyPressed() {
  if (key == ' ' || key == ' ') {
        jump = true;
    } 
}

Btw sorry for messy code :slight_smile:. I have tested many things so there would be remains from those.

I cant reply anymore because reply limit of new users. You can reply here but i will edit my answers to this message. Also if you have discord we can talk there and try to fix my code.


@anon68884268
Do you mean velocity what is in void setup() ?
I disabled those but not anything difference. :confused:


Hmm you are not wrong maybe. Lets check that tomorrow But i will go sleep too in 30mins but if i dont solve this i will need you tomorrow :slight_smile:! But awesome you have community like this. So awesome that people get help to their problems here. @anon68884268 See you tomorrow.

Also guys if you know working method to do jumping. Or you want to help me and make it to my script i will be so happy.


1 Like

I think I found the problem. You’ve got two variables velocity, one time global, and one time in your… Hahmo class. These two variables are never interacting with each other, so they exist and are never used. In your jump() method you are “talking” to the global, but it has no effect, because the vel(ocity) of the Hahmo object is used (which never changes).


In your jump() method you need to change the local PVector vel, which should point upwards (minus y).


Ok, let’s debug. Press Ctrl-F and search for velocity in your code (Wrap around, all tabs, case-sensitive, whole words.). You will (if I am right) only find access called write access and two read access (in your jump method). Press Ctrl-F again and search for vel (With the same settings). You will find only find some read and write accesses, but never interacting with the global velocity variable! But you can’t simply write ha.vel = veloctiy, because ha.vel is from type PVector and velocity from type float!

3 Likes

I did all what u write, but do you know how to fix that. Because if i read right u didnt said how i can fix that. :confused: