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/23

#
i wants to make an image which counts the seconds when the execution is start and remain until the world execution is stop..
depimomo depimomo

2015/4/23

#
hmm like a timer(?)
danpost danpost

2015/4/23

#
What have you tried? Try something -- if you encounter specific problems, then post your issue showing what you tried.
raza raza

2015/4/24

#
import greenfoot.*;
public class Background extends World implements Runnable
{
    boolean isDown = false;
    int key = 0;
    MouseInfo mouse;
    Ball ball=new Ball();
    Thread t=new Thread(this);
    Message messagebox = new Message("");
    static int k=0;// add enemy
    static int p=1;//move ++
    static int q=0;//improving speed
     int i=0,n=0;//for endless loop
    static int sec=0;
    public Background()
    {    
        super(1160, 600, 1); 
        prepare();
        System.gc();
    }
    private void prepare()
    {
        addObject(ball, 604, 279);
    }
    public void act()
    {
        mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mousePressed(null)){
            isDown = true;
            key = mouse.getButton();
            t.start();
         }
    }
    public void run()
    {
               String text = "Time:"; // or not
               Message msgbox = new Message(); // getting the messagebox object
               addObject(msgbox, getWidth()-1120,getHeight()-20); 
             
               try
               {
                    for(i=0;i<=n;i++)
                    {
                      Thread.sleep(1500);
                      for(int f=0;f<=i;f++){
                          msgbox.setText(text+i+"s");
                       }
                      for(int l=0;l<=i;l++){ 
                      if(k==i && k==l)
                      {
                          addObject(new Enemy(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));//after 7 seconds new actor will be added      
                          k=k+7;
                      }
                      for(int a=0;a<=l;a++)
                      if(q==i && q==a && q==l)
                      {
                          p++;//after 20 seconds enemy moving speed will increse
                          q=q+20;
                      }
                    }
                      n++;
              }
            }
            
              catch(Exception e)
               {
               //System.out.println("Exception"+e);   
               }
    }
    }
whenever i run this code ,after few seconds error will occure memory heap or out of memory.. i am trying to make the things like after 7 seconds enemy will added in the world and after 20 seconds enemy moving speed will increse.. and also second counter will show at the corner of the world..
raza raza

2015/4/24

#
i dont know how to use timer class and their ,that why use this kind of for loops and thread ..p:
danpost danpost

2015/4/24

#
raza wrote...
i dont know how to use timer class and their ,that why use this kind of for loops and thread ..p:
The thing is, you do not need the timer class and you do not need any for loops -- and you certainly do not need to use threads. Greenfoot provides the initial running thread which allows you to provide code for each act cycle. All you need is one or two int fields (one to count act cycles and maybe another to track groups of act cycles) and one actor to display the current time. Start with basic class (no timer fields or threads):
import greenfoot.*;

public class Background extends World
{

    public Background()
    {    
        super(1160, 600, 1); 
        addObject(new Ball(), 604, 279);
    }

    public void act()
    {

    }
}
Okay, with this we can create a world with a ball in it. We can now start with the timer. First, we need an int field to count the act cycles. So, you would:
// insert at line 5
private int actCounter;
// add at line 14
actCounter++;
The value of 'actCounter' will now increment by one every act cycle. Now, at normal scenario speeds (with the speed slider at 50, the default speed), about 55 act cycles will occur every second. So the 'int' value of 'actCounter/55' will be the number of running seconds of the scenario and the image of the time display should change when 'actCounter%55' equals zero. So, after what we just added at line 14, add the following line:
if (actCounter%55 == 0) updateTimeDisplay();
and add the following after the 'act' method (before line 16 above):
private void updateTimeDisplay()
{

}
Now we need an actor to display the time so we can update its image in the 'updateTimeDisplay' method. I see you have a Message class (which may be suitable), however without seeing its code, I cannot rightly tell how I can use it. This is no big deal, as you only need a simple actor to display the time. Create a new subclass of Actor called 'SimpleActor' and replace ALL of the code within the class with the following:
public class SimpleActor extends greenfoot.Actor {}
(yes, that one line should be the only line of code within the class). We will need to keep a reference to the actor of this class that will be used to display the time; so, insert the following around line 5:
private Actor saTime = new SimpleActor();
I prefixed the name with 'sa' to show it is a SimpleActor object. Next, we need to set the initial image of the actor and add it into the world. So, after the 'super' call in the world constructor, insert the following lines:
updateTimeDisplay();
addObject(saTime, 40, 580);
Finally, we can add the code to give an image to the actor within the 'updateTimeDisplay' method:
int time = actCounter/55;
GreenfootImage image = new GreenfootImage("Time: "+time, 24, null, null);
saTime.setImage(image);
Now you can adjust your act method to check for a time of 7 and 20. Make the following change:
// replace this line in the act method
if (actCounter%55 == 0) updateTimeDisplay();
// with these
if (actCounter%55 == 0)
{
    updateTimeDisplay();

}
You want to add your checks for those specific time value at line 7 in the last snippet:
if (actCounter/55 == 7) addObject(new Enemy(), <x>, <y>);
if (actCounter/55 == 20) increaseEnemySpeed();
Either add the method to increase the enemy speed or replace the method call with the code that does it. That should do it!
raza raza

2015/4/24

#
thanks danpost..!!!its working but when i run this code after 1 to 5 second perfomance of the world or moving things is goes down its like gliching .. what i do for that.. this is world class
import greenfoot.*;
public class Background extends World 
{
    boolean isDown = false;
    int key = 0;
    MouseInfo mouse;
    Ball ball=new Ball();
    Message messagebox = new Message("");
    Message msgbox = new Message();
    private int actCounter;
    static int k=1;// add enemy
    static int p=1;//move ++
    static int q=1;//improving speed
    private Actor m=new Message();
    int time;
    public Background()
    {    
        super(1160, 600, 1);
        addObject(m,40,580);
        prepare();
        System.gc();
    }
    private void prepare()
    {
        addObject(ball, 604, 279);
        addObject(msgbox, getWidth()-1120,getHeight()-20);
    }
    public void act()
    {
        actCounter++;
        
        String text="Time :"+time;
        if(actCounter%55==0)
        {
            time=actCounter/55;
            msgbox.setText(text);
            addEnemy();
            improvingSpeed();
        }
    }
    public void addEnemy()
    {
        if(time==k)
            {
                addObject(new Enemy(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));//after 7 seconds new actor will be added      
                k=k+7;
            }
    }
    public void improvingSpeed()
    {
        if(time==q)
            {
                 p++;//after 20 seconds enemy moving speed will increse
                 q=q+20;
            }
    }
}
this is Message class
import greenfoot.*;
import java.awt.Color;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Message here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Message extends Actor
{
    public Message()
    {
        updateImage("");
    }
     
    public Message(String text)
    {
        updateImage(text);
    }
     
    public void setText(String text)
    {
        updateImage(text);
    }
     
    private void updateImage(String text)
    {
       GreenfootImage textImage = new GreenfootImage(text, 20, Color.black, new Color(0, 0,0, 0));
        GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
        image.drawRect(0, 0, image.getWidth()-1, image.getHeight()-1);
        image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
        setImage(image);
    }
}
raza raza

2015/4/24

#
Enemy class if you need
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Background b=new Background();
        int s=b.p;
        turn();
        moveto(s);
        turn(360);
    }    
    public void turn()
    {
        if(isTouching(null)==true)
        {
            turn(Greenfoot.getRandomNumber(70)-50);
        }
        if(getX()<=10 ||getX()>=getWorld().getWidth()-10)
        {
            turn(Greenfoot.getRandomNumber(360)-90);
        }
        if(getY()<=10||getY()>=getWorld().getHeight()-10)
        {
           turn(Greenfoot.getRandomNumber(360)-90);
        }
    }
    public void moveto(int s)
    {
        move(s);
    }
}
danpost danpost

2015/4/24

#
There is the problematic code -- in the Enemy class act method, you are creating a new Background world object every act cycle. So, once an enemy is placed into the initial world, new worlds will be created constantly (causing the glitching). You do not want to create any new worlds there (line 17 of the Enemy class); you want to get a reference to the world that the Enemy object is already in (a previously created world -- so do not use the 'new' keyword here). The 'getWorld' method of the Actor class will return a reference to that world, but only referenced as type World -- not of type Background (which is needed for you to access any fields or methods in that class). So, change line 17 to this:
Background b = (Background) getWorld();
raza raza

2015/4/24

#
Thanks a lot danpost its now working ...thanks ... can u tell me how to load 5 images one by one . and if one image is load in main memory previous image will delete from the memory ,that means whenver one image is load no any other image will load in main memory.
raza raza

2015/4/24

#
i wants to make an animation. from those 5 images...plz i didn't do any code for that..
danpost danpost

2015/4/24

#
raza wrote...
can u tell me how to load 5 images one by one . and if one image is load in main memory previous image will delete from the memory ,that means whenver one image is load no any other image will load in main memory.
What are you trying to animate? (the world background or an actor) What images are you using? (list their filenames) What have you tried? (show some attempt)
raza raza

2015/4/24

#
im trying to animate an image which having the same size...and those image extension is png. image name is i1,i2,i3,i4,i5; this animation is occur when two actor is touch with each other.. and execution of greenfoot will stop.!
if(isTouching(Enemy.class))
        {
            GreenfootImage image = new GreenfootImage("images/blast.png");
            world=getWorld();
            world.removeObject(this);
            world.getBackground().drawImage(image,mouse.getX(),mouse.getY());
            Greenfoot.stop();
        }
raza raza

2015/4/24

#
i wants to load an animation instead of an image...
danpost danpost

2015/4/24

#
You will need to change line 6 to probably calling a method which will run the animation. This particular animation can be completely run in one run of the method (there is no need to process each step of the animation is separate act cycles since the game is over and it will not matter that all actors are prevented from acting during the animation). In the called method, iterate through each image using a delay after each is set. A 'for' loop can easily be used considering the naming of the image filenames.
There are more replies on the next page.
1
2
3