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

2017/12/12

Invisible Object Problem

donysamdhila donysamdhila

2017/12/12

#
Hey There. I Want to make an object invisible with time limit. And then when the invisible time limit is over, that's object will be visible. Visible is also with time limit. But when i tried this code, my object is visible for a few seconds and then become invisible for ever.
public void gerak()
    {
        move(direction);
        if (isAtEdge())
        {
            direction = -direction;
        }
        if (visible>0)
        {
            World world;
            world = getWorld();
            getImage().setTransparency(255);
            
            visible--;
            if(visible == 0)
            {
                invisible = 100;
                getImage().setTransparency(0);
                invisible--;
                if(invisible == 0)
            {
                getImage().setTransparency(255);
                visible = 100;
            }
            }
        }
    }
danpost danpost

2017/12/12

#
The first thing I would consider is using only one timer because both should never be running at the same time. The timer then should run twice as long and a change in the image should occur at two values of the timer. So, if the timer starts at zero, then at 100 and at 200 (or when the remainder is zero after dividing by 100) is when things happen. At 200, the value of the timer should be reset to zero. This can be done a couple of ways. The easier way would be as follows:
timer = (timer+1)%200;
The timer will count from 0 to 199, and continuously repeat. After the timer is incremented, its value is checked:
if (timer%100 == 0)
The important two value of the timer are now 100 and 0. Now, the transparency value is an integer and will be either 255 or 0. Also, the important timer values, when divided by 100 will be either 1 or 0. Multiplying 'timer/100' or '1-timer/100' by 255 will give the two values needed for the transparency. So, in all, we get this to replace lines 8 through 26:
timer = (timer+1)%200;
if (timer%100 == 0) getImage().setTransparency(255*(timer/100));
If your actors image is to be visible first, add the '1-' in front of 'timer/100'. Of course, you will get rid of the 'invisible' and 'visible' timers and add a single 'timer' timer.
donysamdhila donysamdhila

2017/12/12

#
@danpost still not working, maybe there is a fault with my implementation. Can you make full code for me ?
danpost danpost

2017/12/12

#
donysamdhila wrote...
@danpost still not working, maybe there is a fault with my implementation. Can you make full code for me ?
There is nothing more I can do without seeing more of your current codes. Start by showing the class in its entirety.
donysamdhila donysamdhila

2017/12/12

#
There is my default code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Boss1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boss2 extends Boss
{
    /**
     * Act - do whatever the Boss1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean jatuh;
    private int y=3;
    private int yspeed=1;
    private int up=20;
    
    private int atime=0;
    private boolean right;
    
    private int direction = -15; //move left
    private int invisible = 0;
    private int visible = 100;
    //private int shootingcounter = 800; // Delay shooting
    public void act() 
    {
        damage();
        gerak();
        //shootingcounter --;
        cek();
    }    
    public void gerak()
    {
        move(direction);
        //move_left();
        if (isAtEdge())
        {
            direction = -direction;
        }
        if (visible>0)
        {
            World world;
            world = getWorld();
            getImage().setTransparency(255);
            
            visible--;
            if(visible == 0)
            {
                invisible = 100;
                getImage().setTransparency(0);
                invisible--;
                if(invisible == 0)
            {
                getImage().setTransparency(255);
                visible = 100;
            }
            }
        }
    }
    public void move_right()
    {
        right=true;
        //setLocation(getX () +7, getY());
        atime=atime+1;
        if (atime==11) atime=0;
        if (atime==0) setImage("p3_stand.png");
        if (atime==1) setImage("p3_walk01.png");
        if (atime==2) setImage("p3_walk02.png");
        if (atime==3) setImage("p3_walk03.png");
        if (atime==4) setImage("p3_walk04.png");
        if (atime==5) setImage("p3_walk05.png");
        if (atime==6) setImage("p3_walk06.png");
        if (atime==7) setImage("p3_walk07.png");
        if (atime==8) setImage("p3_walk08.png");
        if (atime==9) setImage("p3_walk09.png");
        if (atime==10) setImage("p3_walk10.png");
        if (atime==11) setImage("p3_walk11.png");
        //if (walkri==false) getImage().mirrorHorizontally();
    }
    public void move_left()
    {
        right=false;
        //setLocation(getX ()-7, getY());
        atime=atime+1;
        if (atime==11) atime=0;
        if (atime==0) setImage("p3_stand.png");
        if (atime==1) setImage("p3_walk01_left.png");
        if (atime==2) setImage("p3_walk02_left.png");
        if (atime==3) setImage("p3_walk03_left.png");
        if (atime==4) setImage("p3_walk04_left.png");
        if (atime==5) setImage("p3_walk05_left.png");
        if (atime==6) setImage("p3_walk06_left.png");
        if (atime==7) setImage("p3_walk07_left.png");
        if (atime==8) setImage("p3_walk08_left.png");
        if (atime==9) setImage("p3_walk09_left.png");
        if (atime==10) setImage("p3_walk10_left.png");
        if (atime==11) setImage("p3_walk11_left.png");
    }
    public void jump()
    {
        if(jatuh==false)
        {
            y=-up;
            bawah();
        }
    }
    public void bawah()
    {
        jatuh=true;
        setLocation(getX(),getY()+y);
        y=y+yspeed;
    }
     public boolean tanah()
    {
        jatuh=false;
        Actor tanah=getOneObjectAtOffset(0,getImage().getHeight()/2-1,Tile2.class);
        return tanah!=null;
    }
   public boolean papan()
    {
        jatuh=false;
        Actor pijakan=getOneObjectAtOffset(0,getImage().getHeight()/2-1,Tile1.class);
        return pijakan!=null;
    }
    public void cek()
    {
        if(tanah())
        {
            y=0;
        }
        else if(papan())
        {
            y=0;
        }
        else
        {
            bawah();
        }
    }
}

danpost danpost

2017/12/12

#
Replace lines 42 through 60 with my 2-liner above and replace lines 24 and 25 with this:
private int timer = 0;
Then test it out. If it is still not working, show the revised class (so it can be verified that the changes were done correctly) and explain in what way it is not what you wanted. You may also need to show the Boss class code (since that class is what this class extends and may influence what is going on).
donysamdhila donysamdhila

2017/12/12

#
danpost wrote...
Replace lines 42 through 60 with my 2-liner above and replace lines 24 an 25 with this:
private int timer = 0;
Then test it out. If it is still not working, show the revised class (so it can be verified that the changes were done correctly) and explain in what way it is not what you wanted. You may also need to show the Boss class code (since that class is what this class extends and may influence what is going on).
Thanks !! It's working..
You need to login to post a reply.