Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problems with Mutator method (new problem see last post from me)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 DERASTAT   Members   -  Reputation: 107

Like
0Likes
Like

Posted 10 November 2013 - 02:00 PM

UPDATE: BETTER STYLE

I want to make my charakter move to the clicket spot. so i created deltaMousePlayerX and deltaMousePlayerY to get the diffrent between the clickted spot and the Player.

 

The Problem:

if(mouseinput !=null)
      System.out.println("WORKING"); // here MouseInpt keep being null so i call the wrong objekt?

 

ok now its more clear:

here is the class MouseInput:

public class MouseInput implements java.awt.event.MouseListener
{

    double x;
    double y;
 
    private double deltaMousePlayerX;
    private double deltaMousePlayerY;
    
    private Player p;
    
    public MouseInput(Player p){
        this.p = p;   
    }
  
    public void mouseClicked(MouseEvent e) {
        x=e.getX();// here i get the coordiantes of the clicked spot
        y=e.getY();

        if(p != null){
            deltaMousePlayerX= (p.getX()- x); // here its gives me the diffrents between the clicket spo and teh Player
            deltaMousePlayerY= (p.getY() - y);
           
            System.out.println(deltaMousePlayerX+","+deltaMousePlayerY); // Its printing the right numbers out evrything works when 
        }                                                                 // I click the mouse
    }

   
    public void mousePressed(MouseEvent e) { 
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    
    public double getDeltaMousePlayerX(){     // Getter and setters
        return deltaMousePlayerX;
    }
    public double getDeltaMousePlayerY(){
        return deltaMousePlayerY;
    }
    public void setDeltaMousePlayerX(double x){
        this.deltaMousePlayerX = deltaMousePlayerX;
    }
    public void setDeltaMousePlayerY(double y){
        this.deltaMousePlayerY = deltaMousePlayerY;
    }
}


the instantiate

private Player p; // 
private MouseInput mouseinput;// Mouseinoput is the class above

p = new Player(200, 200 ,this, mouseinput);// The player is visible on the screen
        addMouseListener(new MouseInput(p));

and then the critic part the Player class:

public class Player {

    private double x; // coordinates of the player
    private double y;
    
    private MouseInput mouseinput;
    
    
    
    
    public Player (double x, double y, Biotopwar2D biotopwar2d, MouseInput m){
    
        this.x = x;
        this.y = y;
        this.mouseinput = mouseinput;
    }
    
    public void tick(){
       
        if(mouseinput !=null)
            System.out.println("WORKING"); // here MouseInpt keep being null so i call the wrong objekt?
        
        
        
   } 
}

(i cut some unImportent parts for teh problems)



Sponsor:

#2 molehill mountaineer   Members   -  Reputation: 595

Like
2Likes
Like

Posted 10 November 2013 - 04:10 PM

Hi,

 

I you want people to debug your code you should make it as easy as possible to understand.

- please use the code blocks (<> symbol in editor or [ code ] [ / code ] without the spaces. It will make your post much more readable.

- post the error message so we know where to start looking

- explain how the code is supposed to work - where does MousInput come from when you pass it to player, why are you storing it in the player instance?

- NEVER use single letters are variable names. It makes your code confusing as hell to read. A variable name should reflects its purpose. X and Y are okay since this is mathematical convention, 'm' is a no-no.

 

Since the compiler didn't complain that the variable m was undeclared in the function tick() I assume you have 

private MouseInput m;

somewhere at the top of the parent class. You probably forgot to initialize it. Something along these lines:

m = new MouseInput();

Or maybe you are thinking about variable scope the wrong way, in which case the solution would be something like

 

if(player.getM() != null)
{
 System.out.println(player.getM().getDeltaMousePlayerX()+",");
}

 

If you post some more details I'll be better able to help you fix the error.


Edited by molehill mountaineer, 10 November 2013 - 04:31 PM.


#3 dr01d3k4   Members   -  Reputation: 433

Like
0Likes
Like

Posted 10 November 2013 - 05:26 PM

When you instantiate the MouseInput class, you didn't pass it to the player. You'd have to do something like this:

MouseInput mouseInput = new MouseInput();
Player player = new Player(x, y, biotopwar2d, mouseInput);
addMouseListener(mouseInput);

Falling block colour flood game thing I'm making: http://jsfiddle/dr01d3k4/JHnCV/


#4 DERASTAT   Members   -  Reputation: 107

Like
0Likes
Like

Posted 11 November 2013 - 07:09 AM

 

When you instantiate the MouseInput class, you didn't pass it to the player. You'd have to do something like this:

MouseInput mouseInput = new MouseInput();
Player player = new Player(x, y, biotopwar2d, mouseInput);
addMouseListener(mouseInput);

I tryed : addMouseListener(new MouseInput(p)); and after taht i tryed MouseInput mouseinput = new MouseInput(p); (second i am not sure about that

 

but still mouseinput is null in th Player class

 

PS: i make the Startpost clearer

 

BTW: thanks for the quick answers



#5 dr01d3k4   Members   -  Reputation: 433

Like
0Likes
Like

Posted 11 November 2013 - 12:48 PM

The MouseInput doesn't need to know about the player object. You need to instantiate the MouseInput object before the player and then pass this input to the player and the addMouseListener method.


Falling block colour flood game thing I'm making: http://jsfiddle/dr01d3k4/JHnCV/


#6 DERASTAT   Members   -  Reputation: 107

Like
0Likes
Like

Posted 13 November 2013 - 09:49 AM

The MouseInput doesn't need to know about the player object. You nre the player andeed to instantiate the MouseInput object befo then pass this input to the player and the addMouseListener method.

how to pass an objekt to a class?  because if i didnt initilize it i cant use p.getY for the right onjekt.

mouseinput = new MouseInput();
        p = new Player(200, 200 ,this, mouseinput);
        addMouseListener(mouseinput);

sorry I am new to multi class programming



#7 DERASTAT   Members   -  Reputation: 107

Like
0Likes
Like

Posted 19 November 2013 - 05:19 PM

 

The MouseInput doesn't need to know about the player object. You nre the player andeed to instantiate the MouseInput object befo then pass this input to the player and the addMouseListener method.

how to pass an objekt to a class?  because if i didnt initilize it i cant use p.getY for the right onjekt.

mouseinput = new MouseInput();
        p = new Player(200, 200 ,this, mouseinput);
        addMouseListener(mouseinput);

sorry I am new to multi class programming

 

Zu früh gefreut das Objekt scheint ein anderes zu sein als das Im mouselistener da:

public void tick(){
        
         
        
        if(mouseinput !=null){
            System.out.println(","+ mouseinput.getDeltaMousePlayerX());
            if(mouseinput.getDeltaMousePlayerX() < 0){
            x++;
            System.out.println(","+ mouseinput.getDeltaMousePlayerX()); //bleibt null
            mouseinput.setDeltaMousePlayerX(mouseinput.getDeltaMousePlayerX()+1);
            }
        }
        
        
    }

hie bleibt DeltaMousePlayerX 0






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS