This site requires JavaScript, please enable it in your browser!
Greenfoot back
Moremoneymoreproblems
Moremoneymoreproblems wrote ...

2023/12/5

Pls help

So I'm making this fighting game for my school project, but I am having some trouble with the code. I was able to add all the fighting. but when I added the health bars, I started to get some problems.I have multiple levels, so I made multiple health bars for all the differet worlds, but now my fighting got messed up. public void checkKey() { if (Greenfoot.isKeyDown("a")&& !Greenfoot.isKeyDown("f")&& !Greenfoot.isKeyDown("w")&&!Greenfoot.isKeyDown("c")&& !Greenfoot.isKeyDown("g")) { move(-9); walkTimer++; World world = getWorld(); if (walkTimer==0){ run1.scale(180,180); setImage(run1); } if (walkTimer==4){ run2.scale(180,180); setImage(run2); } if (walkTimer==9){ run3.scale(180,180); setImage(run3); } if (walkTimer==14){ run4.scale(180,180); setImage(run4); } if (walkTimer==18){ run5.scale(180,180); setImage(run5); } if(walkTimer==21){ run6.scale(180,180); setImage(run6); } if (walkTimer==24){ run7.scale(180,180); setImage(run7); walkTimer=0; } } else { if(!Greenfoot.isKeyDown("w")&&!Greenfoot.isKeyDown("a")&& !Greenfoot.isKeyDown("f")&& !Greenfoot.isKeyDown("d")&& !Greenfoot.isKeyDown("g")&& !Greenfoot.isKeyDown("c")) { idle=myGif.getCurrentImage(); idle.scale(220, 220); setImage(idle); } } if (Greenfoot.isKeyDown("d")&&!Greenfoot.isKeyDown("w")&& !Greenfoot.isKeyDown("f")&& !Greenfoot.isKeyDown("g")&& !Greenfoot.isKeyDown("c")) { move(9); walkTimer++; if (walkTimer==0){ runflip1.scale(180,180); setImage(runflip1); } if (walkTimer==4){ runflip2.scale(180,180); setImage(runflip2); } if (walkTimer==9){ runflip3.scale(180,180); setImage(runflip3); } if (walkTimer==14){ runflip4.scale(180,180); setImage(runflip4); } if (walkTimer==18){ runflip5.scale(180,180); setImage(runflip5); } if(walkTimer==21){ runflip6.scale(180,180); setImage(runflip6); } if (walkTimer==24){ runflip7.scale(180,180); setImage(runflip7); walkTimer=0; } } if(!isPunching && Greenfoot.isKeyDown("s") && !Greenfoot.isKeyDown("w")) { hit1=hit.getCurrentImage(); hit1.scale(250,250); setImage(hit1); Actor G = getOneIntersectingObject(Girl.class); /*if(G !=null) { World world = getWorld(); Cave Cave=(Cave)world; Health health=Cave.getHealth(); health.loseHealth(); }*/ if(G !=null) { World world = getWorld(); Forest Forest=(Forest)world; Health3 health3=Forest.getHealth3(); health3.loseHealth3(); } /* if(G !=null) { World world = getWorld(); Sunset Sunset=(Sunset)world; Health5 health5=Sunset.getHealth5(); health5.loseHealth5(); }*/ } if(!isKicking && Greenfoot.isKeyDown("w") && !Greenfoot.isKeyDown("s")){ kick1=kick.getCurrentImage(); kick1.scale(250,250); setImage(kick.getCurrentImage()); Actor G = getOneIntersectingObject(Girl.class); if((G !=null)&&(world==(Forest)world)) { World world = getWorld(); Forest Forest=(Forest)world; Health3 health3=Forest.getHealth3(); health3.loseHealth3(); } if((G !=null)&&(world==(Cave)world)) { Cave Cave=(Cave)getWorld(); Health health=Cave.getHealth(); health.loseHealth(); Score score1=Cave.getScore(); score1.addScore1(); } if((G !=null)&&(world==(Sunset)world)) { World world = getWorld(); Sunset Sunset=(Sunset)world; Health5 health5=Sunset.getHealth5(); health5.loseHealth5(); } if(Greenfoot.isKeyDown("c") && !Greenfoot.isKeyDown("v")) { block.scale(180,180); setImage(block); } } } }
danpost danpost

2023/12/5

#
To check if a world is a specific type, use (for example):
if (world instanceof Cave)
There are quite a few places in your code using something like, or something similar to:
if (world==(Cave)world)
which will fail if not that type of world (cannot cast a non-'Cave" type world as a Cave type). They all need to be changed. I feel there are other places where you casting will fail. Check the type before casting using code similar to the first line of code I provided above:
if (world instanceof Cave) {
    Cave cave = (Cave)world;
    // etc.
danpost danpost

2023/12/5

#
Maybe I am mistaken. Maybe you can elaborate on the health bars. Do the worlds themselves have health -- or all the health bars for the player within that world? If it is the player's health, then it should be the player's health bar (in the class of the player). The object representing the player should be passed from world to world, moving the health bar along with it.
OHHHH, It worked thank you so much. I never knew instanceof existed
nccb nccb

2023/12/5

#
For general info, as of the recent Greenfoot 3.8.0+, you can use the new Java feature for this kind of code, so that:
if (world instanceof Cave) {
    Cave cave = (Cave)world;
    // Do something with cave
}
Can be replaced with:
if (world instanceof Cave cave) {
    // Do something with cave
}
Essentially, you can put a variable name after the type in "instanceof" and it declares that variable for use within the if. It will have the type that you just checked with the instanceof.
You need to login to post a reply.