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

2012/11/28

question..

ader108 ader108

2012/11/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
import javax.swing.*;
import java.util.ArrayList;

public class Player1 extends Actor
{
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;

    private int direction;
    private int leavesEaten;
     boolean shoot = false;
    int x;
    

    public Player1()
    {
        setDirection(EAST);
       
    }
    int coincount = 0;
    Bullet b = new Bullet();
    public int nom = 1;
    public int getNom()
    {
        return nom;
    }
      public void act()
    {
       
        if(processKeys());
        if(foundMONEY())
        {
            ++coincount;
            eatCoin();
        }
        if(Greenfoot.isKeyDown("1"))
              
               getWorld().addObject(b, getX() + 1, getY());
              
         if(foundStore()) {
              
           String Choice;
           int ChoiceFix;
           int Choice1 = 1;
           int Choice2 = 2;
           int Choice3 = 3;
           int Choice4 = 4;
           int Choice5 = 5;
           int Choice6 = 6;
           int Choice7 = 7;
           int Choice8 = 8;
           int choice9 = 9;

     Choice = JOptionPane.showInputDialog(null, "Hello! Welcome to the store!\nYou have " + coincount + " coins!\n1. Buy grey- 2 coins \n2. Buy blue- 2 coins \n3. Buy green- 2 coins \n4. Buy red- 2 coins \n5. Buy white- 2 coins \n6. Buy black- 3 coins \n7. Buy gold- 3 coins \n8. Buy NPC1- 5 coins\n9. Buy ZOMBEH- 10 coins");
        ChoiceFix = Integer.parseInt(Choice);
         Actor Player1 = getOneObjectAtOffset(0, 0, Player1.class);
                World myWorld = getWorld();

           if (ChoiceFix == 1){
               if(coincount < 2){
                JOptionPane.showMessageDialog(null, "Sorry, you do not have enough money to buy this");
               setLocation(getX() + 1, getY());}
               else 
               {--coincount;
               --coincount;
                setImage("greyPlayer.png");
                nom = 1;
                setLocation(getX() + 1, getY());}
            }
this continues on, with each 'skin' setting 'nom' to a different number. my question is, how do i get 'nom' to another class? my current way gives me error 'non-static method getNom() cannot be referenced from a static context'
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import java.awt.*;

public class Zombeh extends Actor
{
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;

    private int direction;
    
    public void Zombeh()
    {
        setDirection(EAST);
      
    }
      public void act()
    {
        int nom = Player1.getNom();
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
It would be helpfull to see the method getNom(). If you got this error because you wanted to get the number this way:
Player1.getNom();
You have to change the method getNom() and the variable nom like this:
private static int nom = 0;

public static int getNom() {
    return nom;
}
But this will only work if you have got only one object of the class Player1 because static means that this variable has the same value in all objects of the type Player1 (thats why you can reach the method using the classname).
ader108 ader108

2012/11/29

#
and.. that worked! thank you! i was under the impression that static made it unchangeable. but i guess not
Gevater_Tod4711 Gevater_Tod4711

2012/11/29

#
no static is not unchageable. thats final.
You need to login to post a reply.