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

2015/6/17

Problems with scaling and get objects

khriz_mg khriz_mg

2015/6/17

#
Hi. I am writting my final project (first experience with programming in Greenfoot), and I have some troubles. The first one is the scaling, I want that the Enemy class go from small to the normal size to make the animation to look like they are getting closer to the screen, but then becomes like pixel. The other trouble is that, when MCombi jumps, the enemy class has to move under MCombi, and if it doesn´t jump, all the enemys has to back a little bit. But it doesn´t happend with my code. Could you help me? Here is the code: (sorry for my engish) Road Class
import greenfoot.*;
public class Road extends World
{
    private boolean a=false;
    public Road()
    {    
       super(800, 600, 1); 
       addObject(new MCombi(),getWidth()/2,getHeight()-80);
       setPaintOrder(MCombi.class,Enemy.class);
    }
    public void act()
    {
        if(!a)
        {
            addEnemy();
        }else
        {            
        }
    }
    public void addEnemy()
    {
        if(Greenfoot.getRandomNumber(100)<3)
        {
            addObject(new Enemy(),400,142);
        }
    }
}
Enemy class
import greenfoot.*;
import java.awt.Image;
public class Enemy extends Actor
{
    private GreenfootImage c1 = new GreenfootImage("combi2.png");
    private GreenfootImage c2 = new GreenfootImage("taxi.png"); 
    private int d=5;
    private int s=0;
    private int f;
    private int x;
    private int y;
    public Enemy()
    {
        int tcar = Greenfoot.getRandomNumber(2);
        if( tcar == 0 ) {
            setImage(c1);
            f=120;
        } else if( tcar == 1 ) {
            setImage(c2);
            f=100;
        }
        GreenfootImage img=getImage();
        img.scale(d,d);  
        setImage(img);
     }
    private int n;
    public void act() 
    {
        Actor enemy=(Enemy)getOneIntersectingObject(Enemy.class);
        if(enemy!=null)
        {
            getWorld().removeObject(this);
        }
        else
        {
            x=getX();
            y=getY();
            if(s==0)
             {
                 n=Greenfoot.getRandomNumber(3);
                 s=1;
            }
            choseDirection(n); 
            getCombi();
            setLocation(x,y);  
            grow();           
            if (getY()>=getWorld().getHeight()-5)
            {
                getWorld().removeObject(this);
            }
        } 
    }    
    public void choseDirection(int mov)
    {
        if(mov==0){
            x=getX();
            y=getY()+4;
        }
        else if(mov==1){
           x=getX()+2;
           y=getY()+4;           
        }
        else if(mov==2){
           x=getX()-2;
           y=getY()+4;
        }
    }
    public void grow()
    {
        if(d<=f){
         d=d+2;}
        if(d%5==0)
        {
            GreenfootImage img=getImage();
            img.scale(d,d);   
            setImage(img);
        }         
    }
     public void getCombi()
    {
        Actor combi=(MCombi)getOneIntersectingObject(MCombi.class);
        if (combi!=null)
        {
           retro();
        }
    }
    public void retro()
    {
        if(n==0){
           x=getX();
           y=getY()-4;
        }
        else if(n==1){
           x=getX()-2;
           y=getY()-4;           
        }
        else if(n==2){
           x=getX()+2;
           y=getY()-4;
        }
    }
}
MCombi class
import greenfoot.*;
public class MCombi extends Actor
{
    private Enemy e;
    private boolean y=false;
    private double speed=0.0;
    private double gravity=0.5;
    public void act() 
    {
        if(Greenfoot.isKeyDown("left")&&getX()>90)
        {
            setLocation(getX()-5,getY());            
        }
        if(Greenfoot.isKeyDown("right")&&getX()<715)
        {
            setLocation(getX()+5,getY());
        }
        jump();
        checkfall();
    } 
    private int count;
    public void jump()
    {
        if(Greenfoot.isKeyDown("space")&&!y&&count==0)
        {
            speed=-11.0; 
            y=true;
            count++;
            fall();
        }
        if(!Greenfoot.isKeyDown("space")){y=false; }   
    }
    public void fall()
    {
        setLocation(getX(),(int)(getY()+speed));
        speed=speed+gravity;
    }
    public void checkfall()
    {
        if(getY()>=getWorld().getHeight()-80){
            speed=0.0;      
            count=0;
        } else { fall();}
    }  
    public void getEnemys()
    {
        Actor enemy=(Enemy)getOneObjectAtOffset(0,1,Enemy.class);
        if (enemy!=null)
        {
           
        }
    }
}
danpost danpost

2015/6/17

#
I suggest you never scale the original image; but, each time, scale a copy of the one you need to use (using 'getImage' returns the same image that you set to the actor, not a copy of it). Rescaling the same image over and over again can cause the unwanted effects you are encountering.
You need to login to post a reply.