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

2012/10/8

Newbie problem

roronoa roronoa

2012/10/8

#
So I wanted to write a card game, I wanted to draw a card after clicking at the deck
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Deck extends Game { public void act() { if (Greenfoot.mousePressed(Deck())) //cannot find symbol - method { addObject(new Card(), 107, 1680); } } }
I know it there can be more errors, but I'm really new and couldn't find the mistake.
erdelf erdelf

2012/10/8

#
what is the code of the method Deck()?
roronoa roronoa

2012/10/8

#
There's nothing by now.
danpost danpost

2012/10/9

#
When the act method is executed in a sub-class of Actor, the 'this' keyword refers to the object (the deck that has already been created, in this case). Therefore
if (Greenfoot.mousePressed(this))
would be what you are needing. Using 'Deck()' (without the 'new' keyword) would execute a method (not the constructor) called 'Deck', if there was one. Your use of 'mousePressed' may not be what you want; 'mouseClicked' might be better.
roronoa roronoa

2012/10/9

#
So this problem is solved, thank you. But I have other problems now. Actor classes: Actor -Cards -Deck -ActionCards -Senator Cards class:
import greenfoot.*;

import java.util.List;
import java.util.ArrayList;

public class Cards extends Actor
{
    int x=87;
}
Deck class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Deck extends Cards
{
    int cardsinhand=0;
    public void shift()
    {
    x=x+20;
    }
    public void drawcards()
    {
        getWorld().addObject(new Card(), x, 1680); 
        shift();
        cardsinhand++;
    }    
    public void act() 
    {
     if (Greenfoot.mouseClicked(this))
     {
        if (cardsinhand>=5)
        {
            drawcards();
        }
        else
        {
            for (int i=cardsinhand; i<5; i++)
            {
                drawcards();
            }
        }
     }
     } 
    }   
Senator class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Senator extends Card
{
    public void shift()
    {
    x=x+20;
    }
    public void act() 
    {
     if (Greenfoot.mouseClicked(this))
     { 
      getWorld().addObject(new Senator(), x, 1680);
      shift();
     } 
    }
}
Problems: 1. How can I make the methods drawcards() and shift() more global to be able to use them in the Deck class and Senator class without copying them? 2. When I have 0 cards in my hand and click on the Deck object I'll draw 5 cards, they will lay next to eachother and then click the Senator object, the drawn card will cover the first card drawn in the previous step. How to fix that?
danpost danpost

2012/10/9

#
(1) Since the Senator and Deck classes are sub-classes of the Card class, you can put those methods in the Card class and they will automatically be usable by both classes. Think of the way you use 'getX()' on an object that is a sub-class of the Actor class. You do not have the method in the sub-class, but are able to use it there. As well as 'public' (which can be called from another actor class or the world) and 'private' (which can only be called from the class it resides) for method accessability, there is a 'protected' which can be called from ANY class that is of that class type (from within the class it resides, as well as from any sub-class of it). (2) I think if you got rid of 'x' altogether and just used '87 + cardsinhand * 20' as the x-location to place the new card object, it would resolve this problem. This means removing the 'Shift()' method as well. (Error) replace 'this' with 'Senator'. (I think that is what you wanted)
You need to login to post a reply.