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

2015/4/23

plz help me..!!

1
2
3
raza raza

2015/4/24

#
Thank you danpost I will write a code of run mathod as you said to me.. and i will share it as soon as possible..
raza raza

2015/4/25

#
if(isTouching(Enemy.class))
        {
            GreenfootImage image = new GreenfootImage("images/blast.png");
            world=getWorld();
            world.removeObject(this);
            run();
            //world.getBackground().drawImage(image,mouse.getX(),mouse.getY());
            
        }
public void run()
    {
        GreenfootImage i1 = new GreenfootImage("images/I1.png");
        GreenfootImage i2 = new GreenfootImage("images/I2.png");
        GreenfootImage i3 = new GreenfootImage("images/I3.png");
        GreenfootImage i4 = new GreenfootImage("images/I4.png");
        for(int i=1;i<=4;i++)
        {
            if(i==1)world.getBackground().drawImage(i1,mouse.getX(),mouse.getY());
            if(i==2)world.getBackground().drawImage(i2,mouse.getX(),mouse.getY());
            if(i==3)world.getBackground().drawImage(i3,mouse.getX(),mouse.getY());
            if(i==4)world.getBackground().drawImage(i4,mouse.getX(),mouse.getY());
        }
        Greenfoot.stop();
    }
i used this code to load an image one by one but when its run previous image is overlapped by next image ..what i do to delete a previous image and each is image is load in the peace of interval ....
raza raza

2015/4/25

#
when is use Thread images are loaded in interval of one second but its still overlapping .what code is prefer to delete an image.
if(isTouching(Enemy.class))
        {
            GreenfootImage image = new GreenfootImage("images/blast.png");
            world=getWorld();
            world.removeObject(this);
            t.start();
            //world.getBackground().drawImage(image,mouse.getX(),mouse.getY());
            
        }
this is run method
public void run()
    {
        GreenfootImage i1 = new GreenfootImage("images/I1.png");
        GreenfootImage i2 = new GreenfootImage("images/I2.png");
        GreenfootImage i3 = new GreenfootImage("images/I3.png");
        GreenfootImage i4 = new GreenfootImage("images/I4.png");
        for(int i=1;i<=4;i++)
        {
            try{
            Thread.sleep(1000);
            if(i==1)world.getBackground().drawImage(i1,mouse.getX(),mouse.getY());
            if(i==2)world.getBackground().drawImage(i2,mouse.getX(),mouse.getY());
            if(i==3)world.getBackground().drawImage(i3,mouse.getX(),mouse.getY());
            if(i==4)world.getBackground().drawImage(i4,mouse.getX(),mouse.getY());
           }
           catch(Exception e)
           {
           }
        }
        Greenfoot.stop();
    }
danpost danpost

2015/4/25

#
Again, Thread should not be used. Create an Actor subclass called Blast and code it to show the images in sequence and remove itself (and stop the scenario if needed) when the sequence is done. Then have the actor that creates the blast create a new Blast object and add it into the world when needed. The Blast class would look something like this:
import greenfoot.*;

public class Blast extends Actor
{
    private static final GreenfootImage[] images =
    {
        new GreenfootImage("I1.png"),
        new GreenfootImage("I2.png"),
        new GreenfootImage("I3.png"),
        new GreenfootImage("I4.png")
    };

    private int timer;

    public Blast()
    {
        setImage(images[0]);
    }

    public void act()
    {
        timer++;
        if (timer%55 == 0)
        {
            if (timer/55 == images.length)
            {
                getWorld().removeObject(this);
                Greenfoot.stop();
            }
            else setImage(images[timer/55]);
        }
    }
}
raza raza

2015/4/25

#
Thanks a lot danpost ...i didn't know about array of green foot image ,That automatically remove the previous image ...i did use clear method of that but thats not work properly ..but now its done ...thanks.. and plz can you give an explaination of this code..??

        timer++;
        if (timer%55 == 0)
        {
            if (timer/55 == images.length)
            {
                getWorld().removeObject(this);
                Greenfoot.stop();
            }
            else setImage(images[timer/55]);
        }
danpost danpost

2015/4/25

#
raza wrote...
and plz can you give an explaination of this code..??
Line 1: the timer field counts act cycles (each cycle the value is incremented) Line 2: checks to see if the value of the timer field is an even multiple of 55 ('55' is about the number of act cycles that will execute in one second with a scenario running at normal speed); this determines when the current image has been shown long enough; Line 4: determines if all images have been displayed by comparing which multiple of 55 has been reached to the number of images that are to be displayed Lines 6 and 7: removes the actor from the world and stops the scenario (if all images have been displayed) Line 9: sets the next image to the actor (if more images are to be displayed)
raza raza

2015/4/25

#
Ya I got it Thank you danpost...
raza raza

2015/4/25

#
danpost i need your help again...!!! i am trying to make a fire like, after 20sec fire method is called..this code i have done ...but another thing is its call for 5 sec...after 25 sec we will not be able to fire.. and after 40sec we will able to fire again for 5 sec and so on..
private int actCount;
static int time=0;
int HydraGun=20;
 public void act() 
    {
        actCount++;
        if(actCount%55==0)
        {
            time=actCount/55;
            if(time==HydraGun)
            {
                Fire();
            }
        }
}
Super_Hippo Super_Hippo

2015/4/25

#
Questions: Why is 'time' static'? How does the 'Fire' method look like?
danpost danpost

2015/4/25

#
You will need something like the following line:
if (actCount%(55*25) < (55*5))
except it cannot be placed inside the 'if' block that executes only on the mark of a second unit of time. Your code would be something like this:
private int actCount;

public void act()
{
    actCount++;
    if (actCount%(55*25) < (55*5)) Fire();
}
This says that you can fire for the first 5 seconds of each 25 second interval.
raza raza

2015/4/26

#
Thanks its worked done..!! i got an error in other task...its threw an exception(null pointer exception) when i call a world class method. This is the code of world class.
int time;
String s="HighScore: "+time;
 Label l=new Label();
public Background()
{
super(1160, 600, 1);
prepare();
setIF();
}
private void prepare()
    {
        addObject(new Ball(), 604, 279);
        addObject(msgbox, getWidth()-1120,getHeight()-20);
        addObject(l,getWidth()-95,getHeight()-580);
    }
public void setIF()
    {
        int i=0;
        if(i<=time)
        {
            i=time;
            l.setText("Highscore: "+i,25,Color.green,Color.white);
        }
    }
This is the actor class from where is call a setIF method for update a text;

    Background b1 = (Background) getWorld();
public void act() 
    {
if(isTouching(Enemy.class))
        {
            Greenfoot.playSound("l.wav");
            b1.setIF();//this is setIf method here exception is thrown..
            world=getWorld();
            getWorld().removeObjects(getWorld().getObjects(Enemy.class));
            getWorld().removeObjects(getWorld().getObjects(Message.class));
            getWorld().removeObject(this);
            world.addObject(new Blast(),mouse.getX(),mouse.getY());
        }
}
i think that exception is throw because of.."when its call from an actor class control is not going for execute that method its going for next line to execute or when its come back after execution of setIF method Ball Actor is removed from the world..thats why its give an error" so what can i do to for update a image before actor is removed from the world....
danpost danpost

2015/4/26

#
Line 1 will never get a world object. The field is assigned its value before the actor is placed into the world, so its value will be 'null'. Move the line to inside the 'if(isTouching...)' block as the first line in the block.
raza raza

2015/4/26

#
yes its worked.... One more thing ,that is i didn't ever do that .. when i compile the scenarios the value of the High score will remain same that is we have before the compilation. in short i wants to save the value of high score ...and its will increment when greater value is come... what i have to do for that...
danpost danpost

2015/4/26

#
Compiling the project resets ALL fields. Any data you may want to keep will need to be saved in a separate file on a storage device.
raza raza

2015/4/27

#
Nd how can i access that field while i running the scenario.....?
There are more replies on the next page.
1
2
3