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

2016/5/30

Help With Danpost's Infinite Scrolling Background

tuckalow tuckalow

2016/5/30

#
Hey guys, I am trying to code a game where the player is centered at the screen and, using Danpost's Infinite Scrolling Background (Opens in Greenfoot as "continuous scroller test") I have gotten it to work. I have other actors that are my enemies. Here's what I need help with: I would like to make the enemies spawn outside of the height and width of the screen (offscreen). I would like to have the enemies move off screen/out of view without seeing them on the edge of the screen(move completely out of view but still be there). I would like to make my enemies move around randomly WITHOUT turning (rotating). For this, I have tried multiple things. I tried setting their number to a random rotation and then setting it back to 0 (didn't work), and I tried having a random number between 0 and 4 choose one of four scenarios: turn left, turn right, go up, go down. This works, but, once as soon as I implement the code to make these turns (using setLocation), the enemies do not run off of the screen correctly as they had before. As I run around, I seem to drag the enemies sometimes. This happens when the enemies are running downwards and I move sideways and catch them on the side of the screen, or when they are running sideways and I move vertically and catch them on the top or bottom. Does anyone know how to fix the third problem in particular? I guess this is a question mainly for Danpost, as he made the Infinite Scrolling Background. I will attach the code that, when implemented, makes the enemies get dragged.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
if(Greenfoot.getRandomNumber(100) == 1)
            {
            int random = Greenfoot.getRandomNumber(4);
            if(random == 1)
            {
            one = true;
            two = false;
            three = false;
            four = false;
            }
 
            if(random == 2)
            {
            one = false;
            two = true;
            three = false;
            four = false;
            }
 
            if(random == 3)
            {
            one = false;
            two = false;
            three = true;
            four = false;
            }
 
            if(random ==4)
            {
            one = false;
            two = false;
            three = false;
            four = true;
            }
 
            }
            if(one)this.setLocation(getX(), getY() +(int)3);
            if(two)this.setLocation(getX(), getY() -(int)3);
            if(three)this.setLocation(getX()+ (int)3, getY());
            if(four)this.setLocation(getX()-(int)3, getY());
            }
Thank you.
danpost danpost

2016/5/30

#
Apparently, my Scroll class for infinite fixed scrolling only allowed for 'move' and not 'setLocation (something I will have to work on at some point). However, you can replace your code above with the following and it should work fine:
1
2
3
4
5
6
7
8
if (Greenfoot.getRandomNumber(100) == 1)
{
    int random = Greenfoot.getRandomNumber(4);
    int dx = 0;
    int dy = 0;
    if (random%2 == 0) dx = 1-random; else dy = 2-random;
    ((Scroll)getWorld()).moveObj(this, dx*3, dy*3);
}
It is not really a problem that I did not implement a 'setLocation' type of method. The 'moveObj' method can be used in place of it as follows:
1
2
3
4
5
// let us say you had this
setLocation(getX()+dx, getY()+dy);
 
// changing it to this would work in an infinite scrolling world
((Scroll)getWorld()).moveObj(this, dx, dy);
or
1
2
3
4
5
// if you had this
setLocation(100, 100);
 
// change it to this
((Scroll)getWorld()).moveObj(this, 100-getX(), 100-getY());
tuckalow tuckalow

2016/6/7

#
Thank you Danpost! Do you think you would know how to solve the other problems? The actors can move around randomly but when they move offscreen they are still visible on the edges of the screen. Also, I would like to know if it is possible to spawn actors (using moveObj()/other method) off of the visible screen. What I am trying to accomplish is a world bigger than what the player sees. Where there are enemies off the screen and, once you run around, you can encounter them. Any help would be greatly appreciated, thank you very much.
danpost danpost

2016/6/7

#
You could create an unbounded world by adding a 'false' parameter in the super call to the World class: public World(int width, int height, int cellsize, boolean bounded) The above line is the signature of an alternate constructor for the World class which allows you to state whether the world is bounded (prevents actors from leaving the world window) or unbounded (allowing actors to go beyond the limits of the window). So, for example:
1
2
3
4
// instead of
super(1000, 600, 1);
// you would use
super(1000, 600, 1, false);
You need to login to post a reply.