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

2012/4/11

mouse clicking

tylers tylers

2012/4/11

#
whats wrong with this code?
public void act()
    {
        if(Greenfoot.mousePressed(a))
        {
            start1();
            
        }
        
        
        
        make();

    }
I had a go at not clicking on the image but clicked on something else annoyingly it did what in that code. help?
Duta Duta

2012/4/11

#
I assume a is a reference on the correct actor? There's nothing syntactically incorrect in that code. On another point, your code could be written as:
public void act() {
    if(Greenfoot.mousePressed(a)) start1();
    make();
}
tylers tylers

2012/4/11

#
yer a is to the right actor
danpost danpost

2012/4/11

#
It is possible that 'a' is not initialized (which means it is 'null'), which would explain why when you click elsewhere, it executes.
tylers tylers

2012/4/11

#
so how can i fix that?
danpost danpost

2012/4/11

#
What class is your given act() method above in, and how and where is 'a' declared (code please).
tylers tylers

2012/4/11

#
this is where "a" is declared
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;
import java.awt.Point;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
 * Write a description of class moneyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class moneyWorld extends World
{
    //the money
    static int pounds = 0;
    static int pennys = 00;
    static int poundsc = 0;
    static int pennysc = 00;
    boolean start = true;
    GreenfootImage g ;
    add a;

    boolean l =  false;
    boolean minus1 = false;
    JFrame frame;
    int n;
    /**
     * Constructor for objects of class moneyWorld.
     * 
     */
    public moneyWorld()
    {    
act() is in the world
danpost danpost

2012/4/11

#
Look at line 22; 'add' is not a type for a variable. Change it to the type of Actor you are mousePressing on. (I guess I could be mistaken, and 'add' is the name you gave for a sub-class of Actor :?). I will assume that the act() method given above is also in the world class.
tylers tylers

2012/4/11

#
add is what i called a sub-class
danpost danpost

2012/4/11

#
OK, so I see where you declared 'a' a variable on type 'add'. Now, where did you do 'a = new add()'?
tylers tylers

2012/4/12

#
addObject (new add(),300,200);
tylers tylers

2012/4/12

#
oh thanks i manage to repair it tit works now. i needed to do this.
a = new add();
        addObject (a,300,200);
You need to login to post a reply.