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

2012/4/23

Letter game constructor confusion

chris4800 chris4800

2012/4/23

#
I'm trying to create a scenario which creates cards that turn over when the proper key is hit, but I can't seem to understand what I'm doing wrong in actually adding new cards to the world. the is the code for the LetterGame world class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class LetterGame here. * * @author (your name) * @version (a version number or a date) */ public class LetterGame extends World { private Selector selector; private Message message; private String imageNamesFirstRow = {"ambulance.png", "butterfly.png", "elephant.png"}; private String imageNamesSecondRow = {"frog.png", "house-03.png", "kangaroo.png"}; private String imageNamesThirdRow = {"pig.png", "tent.png", "spider.png"}; /** * Constructor for objects of class LetterGame. * */ public LetterGame() { // Create a new world with 500x500 cells with a cell size of 1x1 pixels. super(500, 500, 1); //This line creates the selector object - DO NOT remove or change this line. selector = new Selector(this); //Place your code for the constructor after this comment. addObject( new Message(), 250, 425); message = new Message(); //card = new Card(); for (int i = 0; i <3; i++) { addObject(new Card(selector, imageNamesFirstRow, imageNamesFirstRow.charAt(0), 39, 100)); } } public void act() { selector.newCard(); } public void showGuessMessage() { message.showGuessImage(); } public void showCorrectMessage() { message.showCorrectAnswerImage(); } } and this is the code for the Card class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Card here. * * @author (your name) * @version (a version number or a date) */ public class Card extends Actor { private Selector selector; private boolean faceUp; private GreenfootImage backgroundImage; private GreenfootImage faceImage; private String letter; //private String cardImage = {"ambulance", "butterfly", "elephant", "frog", "house", "kangaroo", "pig", "spider", "tent"}; public Card(Selector sel, String fileN, char let) { selector = sel; sel.addCard(this); createInitialImage(); faceImage =new GreenfootImage(fileN); faceUp = false; letter = "" + let; //Add your code for the constructor after this comment } /** * Act - do whatever the Card wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { } public void turnOver() { //setImage(faceImage); if (faceUp) { setImage(backgroundImage); faceUp = false; } else{ setImage(faceImage); faceUp = true; } } private void createInitialImage() { backgroundImage = new GreenfootImage(100, 100); backgroundImage.setColor(java.awt.Color.BLUE); backgroundImage.fillRect(0, 0, 100, 100); setImage(backgroundImage); } } when I compile the LetterGame class I get the following error message: "Constructor Card in class Card cannot be applied to the given types; required: Selector,java.lang,char found; Selector, java.lang.String,char,int,int reason: actual an..." Any help would be greatly appreciated but thank you for your time nonetheless!
nccb nccb

2012/4/23

#
Your bracketing in this line is wrong:
addObject(new Card(selector, imageNamesFirstRow[i], imageNamesFirstRow[i].charAt(0), 39, 100));
You are passing the X and Y positions to the Card constructor, where you meant to pass them to addObject. Move the second-to-last closing bracket on the line to after the charAt(0) call:
addObject(new Card(selector, imageNamesFirstRow[i], imageNamesFirstRow[i].charAt(0)), 39, 100);
You might need to compare those two lines closely to see the difference! One thing that can help is to move the card construction on to a separate line:
Card c = new Card(selector, imageNamesFirstRow[i], imageNamesFirstRow[i].charAt(0));
addObject(c, 39, 100);
That way, you're less likely to get the brackets confused.
chris4800 chris4800

2012/4/23

#
That's it! Thank you for explaining that. It seems so obvious now haha. Thanks again. It is much appreciated!
You need to login to post a reply.