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

2018/11/28

Istouching Method problem

Ultr0x Ultr0x

2018/11/28

#
Hi, i am new to greenfoot and wanted to make a game. I have been stuck on this one thing is that i want to change a picture of my rocket after touching Pluto class, so that it looks like it lands on it. There is no problem with compiler etc. but it just does not work. Should i also put something to pluto class to work? Here is the code of this method:
public void moveAround()
   {
    if(isTouching(Pluto.class)) 
    {    
       setImage("MoonLander.png");
   }
}
and Pluto:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Solar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pluto extends Actor
{
        
    private GreenfootImage image = new GreenfootImage("Moon.png");
 
    private int scalePercent = 100;
 
    public Pluto(int scalePercent)
    {
        // scale Solar image
        this.scalePercent = scalePercent;
        
        int wide = image.getWidth()*scalePercent/90;
        int high = image.getHeight()*scalePercent/90;
        image.scale(wide, high);
        
        // set initial image to a scaled one
        setImage(image);
    }
    
    public void act() 
    {
        // Add your action code here.
    }    
    
    
}
Zamoht Zamoht

2018/11/28

#
Are you calling moveAround() in your MoonLander/Rocket act() method? Please show the rest of your rocket's code.
Ultr0x Ultr0x

2018/11/28

#
Hi, right now i have this trange console bug, if(isTouching(Pluto.class)) is highlighted but this is roket class code:
import greenfoot.*;  
public class Rocket extends Actor
{
    private int tgtx = 0;
    private int tgty = 0;
    private int jeda = 90;
    private GreenfootImage image = new GreenfootImage("rocket.png");
 
    private int scalePercent = 100;

    public Rocket(int scalePercent)
    {
        // scale Earth image
        this.scalePercent = scalePercent;
        
        int wide = image.getWidth()*scalePercent/100;
        int high = image.getHeight()*scalePercent/100;
        image.scale(wide, high);
        
        moveAround();
        // set initial image to a scaled one
        setImage(image);
    }

    public void addedToWorld (World MyWorld)
    {
        tgtx = getX();
        tgty = getY();
        turn(-110);
        
     
    }
    public void moveAround()
    {
        if(isTouching(Pluto.class))
      {    
       setImage("MoonLander.png");
       GreenfootSound mySound = new GreenfootSound("WereMoving.wav");
       mySound.play();
       
      }
    }
    private int corr = 90;
    
   
            public void act() {
    if (Greenfoot.isKeyDown("left")) {
        turn(-1);
    }
    if (Greenfoot.isKeyDown("right")) {
        turn(1);
    }
    if (Greenfoot.isKeyDown("up")) {
        move(1);
    }
    if (Greenfoot.isKeyDown("down")) {
        move(-1);
    }
  
  }
}
danpost danpost

2018/11/28

#
You are calling moveAround from the constructor, which is executed only once before the created actor is placed into a world.
Zamoht wrote...
Are you calling moveAround() in your MoonLander/Rocket act() method?
This question should point you in the right direction to fix the issue.
You need to login to post a reply.