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

2015/12/2

Error - Scrolling the world

pintoo pintoo

2015/12/2

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

/**
 * Write a description of class doodler here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class doodler  extends Actor
{
    float ys, xs;
    int x,y;
    public static int  currentX;
    public static int  currentY;
    boolean canMove;
    GreenfootImage left = new GreenfootImage("doodler.png");
    GreenfootImage right = new GreenfootImage("doodler.png");
    GreenfootImage shooting = new GreenfootImage("shooting.png");
    GroundFactory groundFactory=new GroundFactory();
    ground Ground = new ground();
    int scrollSpeed;
    public ground Ground2; 
    int hits = 0;
    int x1;
    int x2;
    int y1,y2;
    int groundChance;
    
    public static int groundCount=0;
    /**
     * Act - do whatever the doodler wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public doodler()
    {
        left.mirrorHorizontally();
        shooting.scale(25,40);
        canMove = true;
    }

    protected void addedToWorld(World world)
    {
        x = getX();
        y = getY();
    }

    public doodler(boolean moveable)
    {
        canMove = moveable;
    }

    public void act() 
    {
        x = getX();
        y = getY();
        currentX = getX();
        currentY = getY();
        
        ((doodleWorld) getWorld()).doodleX = getX();

        if(((doodleWorld) getWorld()).fall==false)
        {
            bounce();
        }
        if(canMove)
           { keys();
            getCoin(); }

        if(ys>11)
            ys = 10;

        setLocation(x+(int)xs, y+(int)ys);

        gravity();
        wrapAround();

        if(canMove & y < 400)
            scrollUp();

        if(y>400)
            fall();

        ((doodleWorld) getWorld()).height = hits;

        scout();
    }    

    public void gravity()
    {
        ys += 0.3f;
    }

    public void bounce()
    {
     ground Ground = (ground) getOneIntersectingObject(ground.class);
       // ground Ground = (ground) getObjectsAtOffset(0, 0, ground.class);
        Ground2=Ground;
        if(Ground != null & ys > 0)
        {
            if (Ground.blockType==2)
        {
            fall();
        }
            ys = -10;
            if(canMove & !Ground.hasBeenBounced)
            {
                if(!Ground.isScored && ! Ground.isBrown){ 
                    if(Ground.isSpring){     
                        Score.getInstance().incrementScore(5);
                    }
                    else{
                    Score.getInstance().incrementScore(2);
                    }
                }
                
                Ground.hasBeenBounced=true;
                groundChance=Greenfoot.getRandomNumber(100);
                y1=getRandomY(groundCount%350);
                y2=y1+100;    
                
                //generate brown tiles between 0-30      
                if(groundChance <=  30 )
                {
                    x1=Greenfoot.getRandomNumber(300);
                    x2=getRandomX(x1);
                    groundCount++;
                    //1 => Brown tile
                    getWorld().addObject(groundFactory.getGround(1), x1, y1);
                   
                    groundCount++;
                    
                    getWorld().addObject(groundFactory.getGround(0), x2, y2);
                    scrollUp();
                    
                }
                  
                    //generate spring tile between 30-40 
                else if(groundChance <= 40)   
                    {
                    x1=Greenfoot.getRandomNumber(300);
                    x2=getRandomX(x1);
                    groundCount++;
                    //2 => spring Tile
                    getWorld().addObject(groundFactory.getGround(2), x1, y1);
                    groundCount++;
                    getWorld().addObject(groundFactory.getGround(0), x2, y2);
                    scrollUp();
                    
                }
                else
                    {
                    x1=Greenfoot.getRandomNumber(300);
                    x2=getRandomX(x1);    
                      //generate green tiles   
                    groundCount++;
                    //0 => Green tile
                    getWorld().addObject(groundFactory.getGround(0), x1, y1);
  
                    groundCount++;  
                    getWorld().addObject(groundFactory.getGround(0), x2, y2);
                    scrollUp();
                    
                    }    
                    
            }
        }
    }
    //avoid overlapping of tiles
    public int getRandomX(int randomX1)
    {
        int randomX2=Greenfoot.getRandomNumber(300);
        while(randomX2==randomX1)
        {
         randomX2= Greenfoot.getRandomNumber(300);
        }
        
        return randomX2;
       
    }    

    public int getRandomY(int count)
    {
        //System.out.println(((count*20)+count)/100);
        return ((count*20)+count)/100; 
    }    
    public void keys()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            xs += 0.25f;
            setImage(right);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            xs -= 0.25f;
            setImage(left);
        }
        if(!Greenfoot.isKeyDown("left") & !Greenfoot.isKeyDown("right") & xs != 0)
        {
            xs = 0;
        }
        if(Greenfoot.isKeyDown("space"))// & getWorld().getObjects(ammo.class).isEmpty())
        {
            setImage(shooting);
            getWorld().addObject(new ammo(), x, y);
        }
        
         if(Greenfoot.isKeyDown("w") )
        {
            setImage(shooting);
            getWorld().addObject(new Bullet(), x, y);
        }
    }

    public void getCoin() {
    Actor coin = getOneObjectAtOffset(0, 0, Coin.class);
    if (coin != null) {
        getWorld().removeObject(coin);
      }
    //count up the value of your counter here;
     }
    public void wrapAround()
    {
        if(x>300)
        {
            setLocation(0, getY());
        }
        if(x<0)
        {
            setLocation(300,getY());
        }
    }

    public void scrollUp()
    {
        //ground Ground = (ground) getOneIntersectingObject(ground.class);
        getObjectsAtOffset(0, 0, ground.class); 
        System.out.println("Scrolling height"+y);
        if(y<=200 & y>100)
        {
         if(Ground2 != null)
        {
        if (Ground2.blockType==3){
            ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
            System.out.println("Spring Block");
                    }
        }
            else
            {
            ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
        }
            ((doodleWorld) getWorld()).scroll = true;
            hits++;
            if(Ground2 != null)
        {
            /* if (Ground2.blockType==3)
            ((doodleWorld) getWorld()).scrollSpeed = (int) -ys*;*/
        }
        }
        else if(y<=100)
        {
            if(Ground != null)
        {
            if (Ground.blockType==3)
            {
            ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
            ((doodleWorld) getWorld()).scrollSpeed++;
            System.out.println("Spring Block");
        }
        }
            else
            {
            ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
        }
            ((doodleWorld) getWorld()).scroll = true;
            hits++;
            if(Ground != null)
        {
            //if (Ground.blockType==3)
           // ((doodleWorld) getWorld()).scrollSpeed = (int) -ys*10;
        }
        }
        else
        {
            ((doodleWorld) getWorld()).scroll = false;
        }

   }

   public void fall()
    {
        ((doodleWorld) getWorld()).fall = true;
        ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
        groundCount=0;
    }

    public void scout()
    {
        if(y<0 & getWorld().getObjects(arrow.class).isEmpty())
        {
            getWorld().addObject(new arrow(), getX(), 20);
        }
        if(y>0 & !getWorld().getObjects(arrow.class).isEmpty())
        {
            getWorld().removeObjects(getWorld().getObjects(arrow.class));
        }
    }
    
    
}
pintoo pintoo

2015/12/2

#
Using the above code , I want to scroll the world as the doodler moves up. But this is working in unexpected way. It scrolls up too high that actor goes out of world and game gets over. Want to limit the scrolling as per following requirements. If Blocktype = 2 then actor should fall(which is happening as expected in the code) If Blocktype = 3 i.e Spring Block , the actor should scroll up higher
danpost danpost

2015/12/2

#
You are trying to control the scrolling of the world from an Actor subclass. Although this may be possible to do, it is not the best way. Code your actor to move as you want (falling and springing as needed) without regard to the world itself. Then, in the world class, determine what scrolling is needed and scroll everything there. Keeping a reference to the doodler in the World class will help immensely in the coding.
You need to login to post a reply.