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

2015/1/4

Relative movement issue with scrolling world

1
2
Dillybar Dillybar

2015/1/4

#
Hi, I am using danpost's SWorld scenario to make my world scroll. I have encountered an issue with it that I am unsure of how to overcome. Throughout the scrolling world, I add objects to the world and they appear where they should. However, when I add them, they move relative to how my main actor moves. To exemplify, when my main actor moves 5, the objects that were spawned in "run away" from my actor. I have not included any code as I am uncertain on what to post, but If you would like to see any, let me know. When the objects are added to the world, the scroller boolean is set to false, which is necessary to have them appear in the proper place. This error will only occur once I am off of the "Starting Screen" and the scrolling mechanic is put in place
danpost danpost

2015/1/4

#
When you say 'when I add them, they move relative to how my main actor moves', it appears you are saying that this would be the wanted behavior as that would not be having them 'that were spawned in "run away" from my actor'. If you used 'false' when adding these actors, you telling the scrolling system that the actor is not to scroll. It movement should not be influence by the scrolling system (or by the movement of the main actor). I think the first thing you need to do is add the object using 'true' for the boolean for scrolling ('false' is mainly used for displays of controls and stats like score, health, lives, etc.).
Dillybar Dillybar

2015/1/4

#
Oops, I did screw up my vocab initially. My initial thought was to use the boolean true, however, if I use true, The objects appear on the "Starting Screen" rather than the current screen. Thanks for your input
danpost danpost

2015/1/4

#
You said that the boolean was set to false to have the actors appear in the proper place. Maybe you did not realize that you could use something like the following to have one actor spawn another at its location:
addObject(/** actor */, getUnivX()+getX(), getUnivY()+getY());
Of course you can add to (or subtract from) the coordinates hard-coded values to offset the new actor from the one that spawned it. EDIT: guess its not this then (just saw your last post). Give me a second to see what you are saying.
danpost danpost

2015/1/4

#
Or, maybe it is the above in that you were not adding the scrolled amounts to the coordinates when adding the spawned objects into the world.
Dillybar Dillybar

2015/1/4

#
Ah I did not think of that before. That should work, however, when I call getUnivX() it will require an integer in the brackets. What would I put in it for it to work? For the record, this is how I call it but it comes up with an error:
scrolled = ((SWorld)getWorld()).getUnivX();
danpost danpost

2015/1/4

#
Dillybar wrote...
Ah I did not think of that before. That should work, however, when I call getUnivX() it will require an integer in the brackets. What would I put in it for it to work?
Yeah, I guess you will need something there. The methods 'getUnivX' and 'getUnivY' convert the current world coordinate values to universal scrolling values. Put 'getX()' and 'getY()' in the brackets respectively. So, to add an actor above the actor spawning it, you would use something like this:
int univX = ((SWorld)getWorld()).getUnivX(getX());
int univY = ((SWorld)getWorld()).getUnivY(getY());
getWorld().addObject(/* actor */, univX, univY-30);
Dillybar Dillybar

2015/1/4

#
Hmm, I can understand why that work seeing as getX() returns the value for the X on the current screen. However, when I try to implement the code, it does not spawn in at all (as far as I can tell). Here is the code for defining the variable (did not need univY because there is no vertical scrolling)
scrolled = ((SWorld)getWorld()).getUnivX(getX());
Here is the code for adding the ammo:
((SWorld)getWorld()).addObject(new Ammo(), scrolled, getY() - 32, true);
danpost danpost

2015/1/4

#
Ok. They are spawning; but not where they are supposed to. There is a problem with the math in the 'getUnivX' and 'getUnivY' methods. I will post the corrections shortly.
danpost danpost

2015/1/4

#
Wow. I really overcomplicated that. Here are the replacement codes for those methods:
public int getUnivX(int worldX)
{
    return worldX+scrolledX;
}

public int getUnivY(int worldY)
{
    return worldY+scrolledY;
}
Maybe I should have just added 'get' methods for the values of 'scrolledX' and 'scrolledY'. I can see an update to the Scrolling SuperWorld coming.
Dillybar Dillybar

2015/1/4

#
Awesome, tyvm danpost :). That fixes my problem!
danpost danpost

2015/1/4

#
I seem to be having problems with it myself. An actor should be able to spawn an object at its location by just using 'getX()' and 'getY()'. All the scrolling system does is add the object to the list of scrollables so when scrolling is executed, they can be scrolled.
Dillybar Dillybar

2015/1/4

#
Thats what I had originally thought, but when I was investigating I paused the screen after it had scrolled and used "inspect" on the actor, which returned only the x value for the current screen. This is the number that will be returned when getX() is called. Thus, the object will be spawned at the actors position on the original screen
danpost danpost

2015/1/4

#
Dillybar wrote...
Thus, the object will be spawned at the actors position on the original screen
Whenever you use:
getWorld().addObject(new Ammo(), getX(), getY()-32);
it should spawn a new Ammo object 32 pixels above the center of the object spawning it regardless of where that object currently is. Objects are added to the world using world coordinates, not universal scrolling coordinates.
Dillybar Dillybar

2015/1/4

#
Right but according to the Greenfoot site, getX() will "Return the x-coordinate of the actor's current location." The actors current location is not the overall X scrolled, but the X as it is on the screen
There are more replies on the next page.
1
2