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

2019/12/12

Trouble with second character and Background

1
2
Paciman Paciman

2019/12/12

#
So I've basically made a game with a background that follows my main Actor (Megaman). The game was quite boring to play alone so I've decided to make a second character. The only issue I have, is that there are actors in my game that eat my Megaman and Mega2( second player) and each time they die the game crashes. I don't really understand because I used an If command so that if Megaman dies it instantly focus on the other Actor and it still crashes. Thanks for your help guys.
Paciman Paciman

2019/12/12

#
That's my code if it might help you . { int bgWide = 15000; int bgHigh = 540; public static final int WIDE = 900; public static final int HIGH =540; public static final int CELL =1; private int gameTime; // number of game-time units elapsed private int gameTimer; Scroller scroller; Actor Megaman; Actor Mega2; private GreenfootSound bgMusic = new GreenfootSound("Gov.wav"); public Sky() { super( WIDE, HIGH, CELL, false); GreenfootImage bg = new GreenfootImage("Unknown.jpeg"); scroller = new Scroller(this, bg, bgWide, bgHigh); Megaman = new Megaman(); Mega2 = new Mega2(); addObject(Megaman, bgWide/2, bgHigh/2-230); addObject(Megaman, -100, 200); addObject(Mega2, bgWide/2, bgHigh/2+100); scroll(); Spawn(); } public void act() { if ((Megaman != null && Megaman.getWorld() == this) || (Mega2 != null && Mega2.getWorld() == this) ) scroll(); } private void scroll() { if (Megaman != null) { if (Megaman.getX()>Mega2.getX()) { int dsx = Megaman.getX()-WIDE/2; int dsy = Megaman.getY()-HIGH/2; scroller.scroll(dsx, dsy); } else { int dsx = Mega2.getX()-WIDE/2; int dsy = Mega2.getY()-HIGH/2; scroller.scroll(dsx, dsy); }} }
danpost danpost

2019/12/12

#
The 2nd line in your scroll method requires both actors be in the world -- not just one of them.
Paciman Paciman

2019/12/13

#
danpost wrote...
The 2nd line in your scroll method requires both actors be in the world -- not just one of them.
I tried it it still doesn't work. I think that the problem is that it disappears when it gets eaten so we can't usethe getX of Megaman or Mega2. (sorry for taking so long to answer btw)
danpost danpost

2019/12/13

#
Paciman wrote...
I tried it it still doesn't work. I think that the problem is that it disappears when it gets eaten so we can't usethe getX of Megaman or Mega2.
Please show what you tried. (show act and scroll methods)
Paciman Paciman

2019/12/13

#
danpost wrote...
Paciman wrote...
I tried it it still doesn't work. I think that the problem is that it disappears when it gets eaten so we can't usethe getX of Megaman or Mega2.
Please show what you tried. (show act and scroll methods)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Sky here. * * @author (your name) * @version (a version number or a date) */ public class Sky extends World { int bgWide = 15000; int bgHigh = 540; public static final int WIDE = 900; public static final int HIGH =540; public static final int CELL =1; private int gameTime; // number of game-time units elapsed private int gameTimer; Scroller scroller; Actor Megaman; Actor Mega2; private GreenfootSound bgMusic = new GreenfootSound("Gov.wav"); public Sky() { super( WIDE, HIGH, CELL, false); GreenfootImage bg = new GreenfootImage("Unknown.jpeg"); scroller = new Scroller(this, bg, bgWide, bgHigh); Megaman = new Megaman(); Mega2 = new Mega2(); addObject(Megaman, bgWide/2, bgHigh/2-230); addObject(Mega2, bgWide/2, bgHigh/2+100); scroll(); Spawn(); } public void act() { if ((Megaman != null && Megaman.getWorld() == this) || (Mega2 != null && Mega2.getWorld() == this) ) scroll(); //if (Megaman.getWorld() != null) { gameTimer = (gameTimer + 1) % 60; // adjust 6 to suit if (gameTimer == 0) gameTime++; } //else {addObject(gameTime,100,1000);} } private void scroll() { if (Megaman != null && Mega2!= null) { if (Megaman.getX()>Mega2.getX()) { int dsx = Megaman.getX()-WIDE/2; int dsy = Megaman.getY()-HIGH/2; scroller.scroll(dsx, dsy); } else { int dsx = Mega2.getX()-WIDE/2; int dsy = Mega2.getY()-HIGH/2; scroller.scroll(dsx, dsy); }}
Paciman Paciman

2019/12/13

#
I also tried with if (Megaman != null && Mega2!= null) { both aren't working That's the error message if it might help you. java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:713) at greenfoot.Actor.getX(Actor.java:165) at Sky.scroll(Sky.java:51) at Sky.act(Sky.java:39) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
Paciman Paciman

2019/12/13

#
The lines 39 and 51 are the problem
danpost danpost

2019/12/13

#
Paciman wrote...
The lines 39 and 51 are the problem
Yes. Line 39 ensures that one of the two actors is in the world -- not both. However, line 51 requires that both be in the world. Line 50 does nothing as far as ensuring they are in the world; it only ensures that both actors exist (whether in the world or not). Change line 39 to simply:
scroll();
Then as a first course of action in the scroll method, determine if both, only one or neither actor are in the world. There are a total of 4 cases: (0) neither are in world (1) only Megaman is in world (2) only Mega2 is in world (3) both are in world I numbered them that way for a reason. Assigning (1) and (2) that way allows the sum to be (3); and neither, being not (1) and not (2), makes sense to be (0). So:
int men = 0;
if (Megaman != null && Megaman.getWorld() != null) men += 1;
if (Mega2 != null && Mega2.getWorld() != null) men += 2;
if (men == 0) return;
Actor leader = null;
if (men < 3) leader = men == 1 ? Megaman : Mega2;
else leader = Megaman.getX() >= Mega2.getX() ? Megaman : Mega2;
Now you can scroll with respect to leader.
Paciman Paciman

2019/12/13

#
danpost wrote...
Paciman wrote...
The lines 39 and 51 are the problem
Yes. Line 39 ensures that one of the two actors is in the world -- not both. However, line 51 requires that both be in the world. Line 50 does nothing as far as ensuring they are in the world; it only ensures that both actors exist (whether in the world or not). Change line 39 to simply:
scroll();
Then as a first course of action in the scroll method, determine if both, only one or neither actor are in the world. There are a total of 4 cases: (0) neither are in world (1) only Megaman is in world (2) only Mega2 is in world (3) both are in world I numbered them that way for a reason. Assigning (1) and (2) that way allows the sum to be (3); and neither, being not (1) and not (2), makes sense to be (0). So:
int men = 0;
if (Megaman != null && Megaman.getWorld() != null) men += 1;
if (Mega2 != null && Mega2.getWorld() != null) men += 2;
if (men == 0) return;
Actor leader = null;
if (men < 3) leader = men == 1 ? Megaman : Mega2;
else leader = Megaman.getX() >= Mega2.getX() ? Megaman : Mega2;
Now you can scroll with respect to leader.
And where should I put it in the scroll class ?
Paciman Paciman

2019/12/13

#
I don't see where the scrolls comes in
danpost danpost

2019/12/13

#
Paciman wrote...
And where should I put it in the scroll class ?
I wrote:
danpost wrote...
Then as a first course of action in the scroll method,
(not class). The Scroll class should never be modified.
Paciman Paciman

2019/12/13

#
danpost wrote...
Paciman wrote...
And where should I put it in the scroll class ?
I wrote:
danpost wrote...
Then as a first course of action in the scroll method,
(not class). The Scroll class should never be modified.
I wrote it like this, what should i correct ? private void scroll() { int men = 0; if (Megaman != null && Megaman.getWorld() != null) men += 1; if (Mega2 != null && Mega2.getWorld() != null) men += 2; if (men == 0) return; Actor leader = null; if (men < 3) leader = men == 1 ? Megaman : Mega2; else{ leader = Megaman.getX() >= Mega2.getX() ? Megaman : Mega2; if (Megaman.getX()>Mega2.getX()) { int dsx = leader-WIDE/2; int dsy = leader()-HIGH/2; scroller.scroll(dsx, dsy); }} }
danpost danpost

2019/12/13

#
Paciman wrote...
I wrote it like this, what should i correct ? << Code Omitted >>
Remove the last if structure around the scrolling code.
Paciman Paciman

2019/12/13

#
danpost wrote...
Paciman wrote...
I wrote it like this, what should i correct ? << Code Omitted >>
Remove the last if structure around the scrolling code.
Okay it doesn't crash anymore but it doesn't follow Mega 2 if Megaman dies That's how i wrote it : private void scroll() { int men = 0; if (Megaman != null && Megaman.getWorld() != null) men += 1; if (Mega2 != null && Mega2.getWorld() != null) men += 2; if (men == 0) return; Actor leader = null; if (men < 3) leader = men == 1 ? Megaman : Mega2; else{ leader = Megaman.getX() >= Mega2.getX() ? Megaman : Mega2; { int dsx = leader.getX()-WIDE/2; int dsy = 270; scroller.scroll(dsx, dsy); }} }
There are more replies on the next page.
1
2