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

2020/1/13

Code shows error

LightningFast LightningFast

2020/1/13

#
My code is showing error, but not where and what! Main Actor Code: import greenfoot.*; import java.util.List; public class Reka extends Actor { int heroX; int heroY; boolean isHeroFound = false; public Reka () { GreenfootImage image = getImage(); image.scale(100, 100); setImage(image); } public void act() { findHero(); if (isHeroFound == true) pieThrow(); } public void findHero() { isHeroFound = false; List heroFind = getObjectsInRange(1000, Hero.class); Hero hero = new Hero(); hero = (Hero)heroFind.get(0); heroX = hero.getX(); heroY = hero.getY(); isHeroFound = true; } public void pieThrow() { Pie p = new Pie(); if (heroX < p.getX()) { p.PieTurn(-1,heroX); } if (heroX > p.getX()) { //PieTurnLeft ptl = new PieTurnLeft(); p.PieTurn(1,heroX); } if (heroX == p.getX()) { //PieTurnLeft ptl = new PieTurnLeft(); p.PieTurn(180,heroX); } } } Object Code: import greenfoot.*; public class Pie extends Actor { int rotate = 0; int locY = 0; int locX = getX(); public Pie() { GreenfootImage image = getImage(); image.scale(50,50); setImage(image); } public void act() { } public void PieTurn(int dirctn, int heroX) { GreenfootImage im = getImage(); if (rotate != 180) { im.rotate(dirctn); rotate++; } boolean collision = getObjectAtOffset(Hero.class); if (collision == false || locY<540) { locY++; if (locX != heroX) locX = locX + dirctn; setLocation(locX, locY); } } }
danpost danpost

2020/1/13

#
Certainly, the findHero method has problems. You attempt to get world coordinates of a Hero object that is not in the world. At any rate, it is the Hero object that is already in the world that you would want to find, not a different (new) one.
LightningFast LightningFast

2020/1/13

#
So what else would I have to do to pass on the "hero's" x coordinate info to the PieTurn method? I thought by doing this I'd be assigning the new actor as the one in the world using getObjectsInRange. The error actually is showing up in the PieTurn method, where it says " boolean collision = getObjectAtOffset(Hero.class);".
LightningFast LightningFast

2020/1/13

#
My main objective of this is I have a Pie image at the top of the world, at x =475 (the world is 1000x540 pixels), and it gets launched at the direction of the Hero (which will always be at the bottom of the page but the x-location is changed by the user).
LightningFast LightningFast

2020/1/13

#
UPDATE: I did fix all that I was confused about! But now I need help throwing the pie in the direction the Hero was detected and not change its path (it's being launched and its path is fixated at the spot the Hero was located at). Right now the x-coordinate changes accordingly to the x-movement of Hero :(
danpost danpost

2020/1/13

#
LightningFast wrote...
UPDATE: I did fix all that I was confused about! But now I need help throwing the pie in the direction the Hero was detected and not change its path (it's being launched and its path is fixated at the spot the Hero was located at). Right now the x-coordinate changes accordingly to the x-movement of Hero :(
Do not change the rotation (or turn towards anything) in the act method of the bullet.
You need to login to post a reply.