Friday, 15 May 2015

java - Multiple classes in processing -



java - Multiple classes in processing -

i'm making game you're controlling square, , objects spawn in random places, have pick points, while having dodge big squares going side side. right have 2 classes (one enemies - big squares -, , 1 hero), haven't done point scheme yet spawning objects, that's not i'm trying now.

so problem right don't know how create person lose game/a life when touch "enemies" hero. know how without classes, i'd know how when in separate classes.

if explain code , comments how done, help me out lot :) (i read 'extends' classes i'm not sure if should utilize or not).

here's screenshot of game looks @ moment, improve illustrate it:

here's main code page:

hero myhero = new hero(400,480,5); enemies myenemies = new enemies(50,50,10); enemies myenemies2 = new enemies(50,350,15); enemies myenemies3 = new enemies(50,650,12); void setup() { size(900,800); framerate(30); smooth(); } void draw() { background(0); myhero.keypressed(); myenemies.enemydisplay(); myenemies.enemymove(); myenemies2.enemydisplay(); myenemies2.enemymove(); myenemies3.enemydisplay(); myenemies3.enemymove(); }

class 1:

class enemies { float xpos, ypos, speed; enemies(float x, float y, float s) { xpos = x; ypos = y; speed = s; } void enemydisplay() { rect(xpos, ypos, 100, 100); } void enemymove() { xpos += speed; if((xpos > width - 100) || (xpos < 0)) { speed *= -1; } } }

class 2:

class hero { float xpos_, ypos_, speed_; hero(float x, float y, float s) { xpos_ = x; ypos_ = y; speed_ = s; } void keypressed() { if (key == coded) { if (keycode == up) { ypos_ -= speed_; } if (keycode == down) { ypos_ += speed_; } if (keycode == left) { xpos_ -= speed_; } if (keycode == right) { xpos_ += speed_; } } rect(xpos_,ypos_,30,30); } }

i believe question asking involves basic collision detection , object interactions.

i first create enemies list , create / add together elements during setup() call:

list<enemies> enemies = new list<enemies>(); enemies.add(new enemies(50,50,10));

this allows store of enemies under 1 object. draw() method like:

void draw(){ background(0); myhero.keypressed(); for(enemies enemy : enemies) { enemy.enemydisplay(); enemy.enemymove(); if (hero.iscollidingwith(enemy)) // collision method defined in hero object, define in enemies class well, doesn't matter { hero.removehealth(); // method defined in hero removes health } } }

this method in 1 of classes:

public boolean iscolliding(enemies enemy) { // check x , y coordinates of each object }

i hope helps point in right direction.

java collision-detection

No comments:

Post a Comment