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

2022/2/9

Scrolling World

1
2
3
jdkdoen05 jdkdoen05

2022/2/9

#
Hello, I'm in the process of creating a dogfight game. However, I can't get any further with the Scrolling World, the problem is I don't know if it's possible to do it in such a way that I can move the actor freely without changing the controls. private void checkKeypress() { if (Greenfoot.isKeyDown("w")) { move(15); } if (Greenfoot.isKeyDown("s")) { move(-1); } if (Greenfoot.isKeyDown("d")) { setRotation (getRotation() +5); } if (Greenfoot.isKeyDown("a")) { setRotation (getRotation() -5); } if (shotDelay == 0 && Greenfoot.isKeyDown("space")) { shotDelay = 8; shoot(); } }
danpost danpost

2022/2/9

#
jdkdoen05 wrote...
I can't get any further with the Scrolling World, the problem is I don't know if it's possible to do it in such a way that I can move the actor freely without changing the controls.
Please refer to my Scrolling Tutorial scenario, which refers to a class that is demonstrated in my Scroller Class Demos scenario. Note that the actor code is not an issue with this scrolling system.
jdkdoen05 jdkdoen05

2022/2/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * CLASS:OpenScrollWorld
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OpenScrollWorld extends World
{
    public static final int WIDE=800;
    public static final int HIGH=600;
    Scroller scroller;
    Actor scrollActor;
    /**
     * Constructor for objects of class ActorScrollWorld.
     * 
     */
    public ActorScrollWorld()
    {    
        super(WIDE,HIGH,1,false);
        GreenfootImage image=new GreenfootImage("background");
        scroller=new Scroller(this,image);
        scrollActor=new Kampfjet();
        scrollActor.setImage("Kampfjet Links.png");
        addObject(scrollActor,WIDE/2,HIGH/2);
        Actor kampfjet=new Kampfjet();
        kampfjet.setImage("Kampfjet Links.png");
        addObject(kampfjet,WIDE/2,100);
        kampfjet=new Kampfjet();
        kampfjet.setImage("Kampfjet Rechts.png");
        addObject(kampfjet,WIDE/2,500);
        scroller.scroll(0,0);
    }
    public void act()
    {
        if(scrollActor!=null)scroll();else keyScroll();
        if(Greenfoot.mouseClicked(this))scrollActor=null;
    }
    private void keyScroll()
    {
        int dsx=0,dsy=0;
        if(Greenfoot.isKeyDown("right"))dsx++;
        if(Greenfoot.isKeyDown("left"))dsx--;
        if(Greenfoot.isKeyDown("down"))dsy++;
        if(Greenfoot.isKeyDown("up"))dsy--;
        int rate=2;
        scroller.scroll(dsx*rate,dsy*rate);
    }
    private void scroll()
    {
        int loX=100;
        int hiX=WIDE-100;
        int loY=100;
        int hiY=HIGH-100;
        int dsx=0;
        int dsy=0;
        if(scrollActor.getX()<loX)dsx=scrollActor.getX()-loX;
        if(scrollActor.getX()<hiX)dsx=scrollActor.getX()-hiX;
        if(scrollActor.getX()<loY)dsy=scrollActor.getY()-loY;
        if(scrollActor.getX()<hiY)dsy=scrollActor.getY()-hiY;
        int rate=3;
        int signX=(int)Math.signum(dsx),signY=(int)Math.signum(dsy);
        dsx=signX*Math.min(Math.abs(dsx),rate);
        dsy=signY*Math.min(Math.abs(dsy),rate);
        scroller.scroll(dsx,dsy);
    }
}
I dont know what the problem is? Grennfoot saýs it is an invalid method declaration but i did all like in the tutorial.
danpost danpost

2022/2/14

#
jdkdoen05 wrote...
I dont know what the problem is? Grennfoot saýs it is an invalid method declaration but i did all like in the tutorial.
Where? (which line)
jdkdoen05 jdkdoen05

2022/2/14

#
line 19 public ActorScrollWorld()
danpost danpost

2022/2/14

#
jdkdoen05 wrote...
line 19 public ActorScrollWorld()
The class is not called ActorScrollWorld.
danpost danpost

2022/2/14

#
Also, lines 59 and 61 look off. They compare the wrong way.
jdkdoen05 jdkdoen05

2022/2/14

#
it's exactly the same as in your tutorial, or am I just thinking wrong?
danpost danpost

2022/2/14

#
jdkdoen05 wrote...
it's exactly the same as in your tutorial, or am I just thinking wrong?
No -- it is not the same. Look closely at the comparison symbols ("<", ">").
jdkdoen05 jdkdoen05

2022/2/14

#
In which Line? ^^
danpost danpost

2022/2/14

#
jdkdoen05 wrote...
In which Line? ^^
danpost wrote...
Also, lines 59 and 61 look off. They compare the wrong way.
jdkdoen05 jdkdoen05

2022/2/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * CLASS:OpenScrollWorld
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OpenScrollWorld extends World
{
    public static final int WIDE=800;
    public static final int HIGH=600;
    Scroller scroller;
    Actor scrollActor;
    /**
     * Constructor for objects of class ActorScrollWorld.
     * 
     */
    public ActorScrollWorld()
    {    
        super(WIDE,HIGH,1,false);
        GreenfootImage image=new GreenfootImage("background");
        scroller=new Scroller(this,image);
        scrollActor=new Kampfjet();
        scrollActor.setImage("Kampfjet Links.png");
        addObject(scrollActor,WIDE/2,HIGH/2);
        Actor kampfjet=new Kampfjet();
        kampfjet.setImage("Kampfjet Links.png");
        addObject(kampfjet,WIDE/2,100);
        kampfjet=new Kampfjet();
        kampfjet.setImage("Kampfjet Rechts.png");
        addObject(kampfjet,WIDE/2,500);
        scroller.scroll(0,0);
    }
    public void act()
    {
        if(scrollActor!=null)scroll();else keyScroll();
        if(Greenfoot.mouseClicked(this))scrollActor=null;
    }
    private void keyScroll()
    {
        int dsx=0,dsy=0;
        if(Greenfoot.isKeyDown("right"))dsx++;
        if(Greenfoot.isKeyDown("left"))dsx--;
        if(Greenfoot.isKeyDown("down"))dsy++;
        if(Greenfoot.isKeyDown("up"))dsy--;
        int rate=2;
        scroller.scroll(dsx*rate,dsy*rate);
    }
    private void scroll()
    {
        int loX=100;
        int hiX=WIDE-100;
        int loY=100;
        int hiY=HIGH-100;
        int dsx=0;
        int dsy=0;
        if(scrollActor.getX()<loX)dsx=scrollActor.getX()-loX;
        if(scrollActor.getX()>hiX)dsx=scrollActor.getX()-hiX;
        if(scrollActor.getY()<loY)dsy=scrollActor.getY()-loY;
        if(scrollActor.getY()>hiY)dsy=scrollActor.getY()-hiY;
        int rate=3;
        int signX=(int)Math.signum(dsx),signY=(int)Math.signum(dsy);
        dsx=signX*Math.min(Math.abs(dsx),rate);
        dsy=signY*Math.min(Math.abs(dsy),rate);
        scroller.scroll(dsx,dsy);
    }
}
we fixed the errors, however we still have the same error in line 19
danpost danpost

2022/2/14

#
jdkdoen05 wrote...
we still have the same error in line 19
Why? I told you why that was an issue:

danpost wrote...

The class is not called ActorScrollWorld.
jdkdoen05 jdkdoen05

2022/2/15

#
Could you please send us the corresponding demo we are desperate^^
danpost danpost

2022/2/15

#
jdkdoen05 wrote...
Could you please send us the corresponding demo we are desperate^^
It (the error) has nothing to do with the demo. The class is named on line 9 (the names should match).
There are more replies on the next page.
1
2
3