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

2012/2/28

string and counter

Tomc84 Tomc84

2012/2/28

#
I have been having some trouble getting my string concatenation to update on screen Label Txt1 = new Label("john:" + p1.getScore() ); // add text to world + score count addObject(Txt1, 66, 59); Label Txt2 = new Label("joe:" + p2.getScore() ); addObject(Txt2, 66, 453); this is in my pong world but I don't know hoe to update it
danpost danpost

2012/2/28

#
What methods are available for the Label class? If there is a setText(String) method, just calling 'Txt1.setText("john:" + p1.getScore());' and similar for 'joe', should work.
Tomc84 Tomc84

2012/2/29

#
I tried the Txt1.setText("john:" + p1.getScore()); in ball when the score gets updated and it told me "cannot find symbol" for variable p1
TheNightStrider TheNightStrider

2012/2/29

#
My comment isnt probably too helpful, but i would just just the GreenfootImage class, (if it is a class - i think it is anyway)
danpost danpost

2012/2/29

#
In your initial post, you have p1.getScore() and p2.getScore(), so you have p1 and p2 declared somewhere. Maybe where you put the Txt1.setText(...) statement, that p1 and p2 are not declared in that class? @TheNightStrider, yes, of course, GreenfootImage is a class. @Tomc84, Is the Label class something you created or is it something borrowed from a re-usable class source?
Tomc84 Tomc84

2012/2/29

#
Borrowed form. public void setText(String text) { GreenfootImage img = getImage(); img.clear(); img.drawString(text, 0, 20); } in label and p1 and p2 are in the world as variables p1 = new Paddle("a", "s", "John"); // added arguments key control // step 1 and 2 addObject(p1, 350, 30); p2 = new Paddle("left", "right", "Joe"); addObject(p2, 350, 470); in ball I have a mess, I have tried a lot haha... import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ball here. * * @author (your name) * @version (a version number or a date) */ public class Ball extends Actor { private int ySpeed; private int xSpeed; private int wait; //private int random; public Ball() { //ySpeed = 3; //xSpeed = 3; wait = 80; //random = Greenfoot.getRandomNumber(3)+1; ySpeed = Greenfoot.getRandomNumber(3)+1; xSpeed = Greenfoot.getRandomNumber(3)+1; if (Greenfoot.getRandomNumber(2)==0) // Y up or down 50/50 chance { ySpeed = ySpeed *(-1); } if (Greenfoot.getRandomNumber(2)==0) // X right or left 50/50 chance { xSpeed = xSpeed *(-1); } } /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //ballMove(); //bounceOffPaddle(); bouncOffWalls(); if (wait == 0) { ballMove(); } else { wait --; } } // public void ballMove() // { // setLocation(getX() +xSpeed, getY() +ySpeed); // } /** * Move the ball */ public void ballMove() // NEW MOVE METHOD { if (getY() == 0 || getY() == 500-1) { ((PongWorld)getWorld()).updateScore(this); //Txt1.setText("john:" + p1.getScore()); // updateTxtScore(); //Label.setText(getWorld().Txt1); //Label.setText(PongWorld.Txt1); //((PongWorld)getWorld()).updateScore(Label); //((PongWorld)getWorld()).updateLabel(Txt1); //((PongWorld)getWorld()).updateTxt1(this); // GreenfootImage img = getImage(); // img.clear(); // img.drawString(Txt1, 0, 20); setLocation( 350, 250 ); } else { //setLocation (getX(Greenfoot.getRandomNumber(3)+1), getY(Greenfoot.getRandomNumber(3)+1)); //Greenfoot.getRandomNumber(random)<2); //RandomMove(); setLocation(getX() +xSpeed,getY() +ySpeed); //movement bounceOffPaddle(); // new call for bounceOffPaddle } // if (Greenfoot.getRandomNumber(2)==0) // why does this not belong in move? // { // ySpeed = ySpeed *(-1); // } // if (Greenfoot.getRandomNumber(2)==0) // { // xSpeed = xSpeed *(-1); // } } // public void updateTxtScore(int Txt1,int Txt2) // { // // GreenfootImage img = getImage(); // // img.clear(); // // img.drawString(Txt1, 0, 20); // // ((PongWorld)getWorld()).updateLabel(this); // // setImage(this.Txt1); // // Img.clear(); // // Img.drawString(Txt1,0,20); // // setImage(Txt1); // // GreenfootImage img = getImage(); // // img.clear(); // // img.drawString("", 0, 20); // } // public void setText(String text) // { // setImage(Txt1); // Txt1.clear(); // Txt1.drawString(text,0,20); // setImage(Txt1); // } // public void upDateScoreImg() // set text in lable // { // GreenfootImage img = getImage(); // img.clear(); // img.drawString(Txt1, 0, 20); // } // public void RandomMove() // { // if (Greenfoot.getRandomNumber(100)<50); // { // setRotation(getRotation()+Greenfoot.getRandomNumber(360)); // } // } public void bounceOffPaddle() { Paddle paddle = (Paddle) (getOneObjectAtOffset(0, 0, Paddle.class)); // tells ball paddle is here if (paddle != null) { ySpeed = ySpeed * (-1); Greenfoot.playSound("paddle.wav"); } } public void bouncOffWalls() { if(getX() == 0 || getX() == 700-1) // use of logic or to condense two if statements { xSpeed = xSpeed * (-1); Greenfoot.playSound("offWall.wav");// how does this work? } // else ...........WHY NOT USE AN ELSE???? // { // xSpeed = xSpeed * (-1); // } } // public void bouncOffWalls() CAN BE CONDENSED A SINGLE IF STATEMENT...... HOW THOUGH?????? // { // if(getX() == 0) // { // xSpeed = xSpeed +1; // } // if (getX() == 700-1) // { // xSpeed = xSpeed * (-1); // } // }
danpost danpost

2012/2/29

#
Should there not be a 'setImage(img);' as the last statement in setText(String)?
danpost danpost

2012/2/29

#
In the ballMove method of the Ball class replaces instances of p1.getScore() and p2.getScore() with ((PongWorld)getWorld()).p1.getScore() and ((PongWorld)getWorld()).p2.getScore(). This will get the return the scores of the players whose references are in the world.
Tomc84 Tomc84

2012/2/29

#
Like this? public void ballMove() // NEW MOVE METHOD { if (getY() == 0 || getY() == 500-1) { ((PongWorld)getWorld()).updateScore(this); //((PongWorld)getWorld()).p1.getScore(); Txt1.setText("john:" + ((PongWorld)getWorld()).p1.getScore()); //Txt1.setText("john:" + p1.getScore()); // updateTxtScore(); //Label.setText(getWorld().Txt1); //Label.setText(PongWorld.Txt1); //((PongWorld)getWorld()).updateScore(Label); //((PongWorld)getWorld()).updateLabel(Txt1); //((PongWorld)getWorld()).updateTxt1(this); // GreenfootImage img = getImage(); // img.clear(); // img.drawString(Txt1, 0, 20); setLocation( 350, 250 ); } else { //setLocation (getX(Greenfoot.getRandomNumber(3)+1), getY(Greenfoot.getRandomNumber(3)+1)); //Greenfoot.getRandomNumber(random)<2); //RandomMove(); setLocation(getX() +xSpeed,getY() +ySpeed); //movement bounceOffPaddle(); // new call for bounceOffPaddle }
You need to login to post a reply.