Inheritance/Combining Class Variables
in
Programming Questions
•
1 years ago
While going through the program I am trying to make, I realized that I should try to clean it up, or else the code will get much more confusing and very lengthy. I'm trying to make a game basically just like mario, and im manually putting in all of the jumps because im not skilled enough to use it with a map. To make everything work I made booleans of the jump's variables and the character's variables. I only have one jump for now, but once I would get more, it would make much unnecessary work. I got the x variable of the character (nx) just in public, and that works just fine, but now im trying to have the class of jump extend the character class, and its just not working. Here's the code for these
Jump:
- nicolai n1;
- jump j1;
- float x,xvel,xacc;
- boolean[] keys = {
- false, false, false, false
- };
- boolean jumping=false;
- boolean can=true;
- boolean off=true;
- boolean above=false;
- boolean onjump;
- int ground=536;
- float nx=200;
- int jumpy=ground-64;
- boolean hit=false;
- boolean fall=false;
- void setup() {
- size(600, 600);
- x=0;
- xvel=3;
- xacc=0.1;
- n1=new nicolai();
- j1=new jump(500);
- }
- void draw(){
- background(0);
- if(!keys[1] && !keys[2]){
- xvel=3;
- xacc=0;
- }
- if(keys[1] || keys[2]){
- xacc=.1;
- }
- if(xvel>=10){
- xacc=0;
- }
- j1.update();
- n1.display();
- j1.display();
- n1.jump();
- n1.fall();
- void keyPressed() {
- if ( key == CODED ) {
- if ( keyCode == UP ) {
- keys[0] = true;
- if(off){can=true;}
- }
- if ( keyCode == LEFT ) {
- keys[1] = true;
- }
- if ( keyCode == RIGHT ) {
- keys[2] = true;
- }
- if ( keyCode == DOWN) {
- keys[3] = true;
- }
- }
- }
- void keyReleased() {
- if ( key == CODED ) {
- if ( keyCode == UP ) {
- keys[0] = false;
- off=true;
- }
- if ( keyCode == LEFT ) {
- keys[1] = false;
- }
- if ( keyCode == RIGHT ) {
- keys[2] = false;
- }
- if ( keyCode == DOWN) {
- keys[3] = false;
- }
- }
- }
Jump:
- class jump extends nicolai{
- float jx;
- boolean above=false;
- jump(int ix){
- jx=ix;
- }
- void display(){
- imageMode(CORNER);
- image(a,jx,jumpy);
- }
- void update(){
- jx = jx + ((keys[1]&&x<=0&&nx<=200)?xvel:0) -
- ((keys[2]&&x>=-10000&&nx>=400)?xvel:0);
- if(nx>=jx && nx<=jx+193){
- onjump=true;
- }else{onjump=false;}
- if(n1.y<=jumpy-25){
- above=true;
- }else{above=false;}
- if(jx<nx+25 && jx>nx-193){
- if(!above && keys[2]){
- xvel=0;
- xacc=0;
- }
- }
- if(jx>nx-218 && jx<nx-60){
- if(!above && keys[1]){
- xvel=0;
- xacc=0;
- }
- }
- }
- }
- class nicolai extends map1{
- float groundy=ground;
- float y=groundy-25;
- float yvel=-20;
- float grav=3;
- float comparey=ground-64;
- float endingy;
- boolean fall=false;
- void display(){
- imageMode(CENTER);
- xvel=xvel+xacc;
- nx = nx - ((keys[1]&&nx>=200)?xvel:0) +
- ((keys[2]&&nx<=400)?xvel:0);
- if((keys[1] || keys[2]) && !jumping){
- image(f,nx,y,50,50);
- }else{image(d,nx,y,50,50);}
- }
- void jump(){
- if(!jumping && keys[0]){
- jumping=true;
- yvel=-20;
- if(onjump){
- endingy=jumpy-25;
- }else{endingy=groundy-25;}
- }
- if(jumping && can){
- fall=false;
- if(y+yvel<endingy){
- if(keys[0]){
- if(grav>.6){
- grav=grav-.175*grav;
- }
- }
- y=y+yvel;
- yvel=yvel+grav;
- if(onjump){
- endingy=jumpy-25;
- }else{endingy=groundy-25;}
- }else{jumping=false; off=false; y=endingy; fall=true;
- if(keys[0]){can=false;}grav=3;
- }
- }
- }
- void fall(){
- if(fall && !onjump && y<groundy-25){
- yvel=3;
- yvel=yvel+grav;
- y=y+yvel;
- jumping=false;
- if(nx>=j1.jx-25 && keys[1] && above){
- nx=j1.jx-10;
- }
- if(nx<=j1.jx+218 && keys[2] && above){
- nx=j1.jx+203;
- }
- }
- }
- }
1