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

2014/4/22

Help with mouse info

1
2
bizzard bizzard

2014/4/22

#
hello i am working on my project where i have 16 balls in my Pooltable but i want to move only one cueball to hit other 15 balls every time i click the mouse. can you please help me how i make only my cue ball(0) to move using mouse info.
danpost danpost

2014/4/22

#
Is there a way to distinguish (programmatically) one ball from another? or at least the cue ball from the others? Like, do you have them numbered (does the Ball class have a field such that each ball will have a unique value in it)? or do you have a special class for the cue ball (like as a subclass of Ball)? or maybe you have a field in one of your classes (maybe your subclass of World) to retain a reference to the cue ball?
bizzard bizzard

2014/4/22

#
Ball class has a field and each ball have their own number. My cue ball has a number 0 and its a white colored and other ball are 1-15 number. I am not allowed to make separate subclass for cueball. i Have to move only cueball to hit other 15 balls everytime i click the mouse.
danpost danpost

2014/4/22

#
Keep a reference to the cue ball in the world class and detect mouse clicks from the act method of the world class. Then set the speed and direction of the cue ball when mouse is clicked. The Ball class should take care of the rest.
bizzard bizzard

2014/4/22

#
How to write a method, getX and getY of my cueBall? Becuase when i try to use mouse click info with my Cue its complain can't find getX and getY method. PoolTable Cue = ((PoolTable) getWorld()) ; Cue.getcueBall(); if (info != null && Greenfoot.mouseClicked(null)) { double directionX = mouseX - Cue.getX(); double directionY = mouseY - Cue.getY()
danpost danpost

2014/4/22

#
'Cue' is a reference to your world -- at least, that is what your first line does. The second line get a reference to the cue ball; but, what are you doing with it -- nothing. It goes to never-neverland. Try this:
PoolTable table = (PoolTable)getWorld();
Actor cue = table.getcueBall();
Then, you can use 'cue.getX()' and 'cue.getY()' to get the location coordinates of the cue ball. My line 2 above saves the reference in a local field for use later in the method. The two lines can be combined as follows:
Actor cue = ((PoolTable)getWorld()).getcueBall();
bizzard bizzard

2014/4/23

#
Thank you
bizzard bizzard

2014/4/24

#
i keep getting "Incompatible types" table.getcueBall(); when i try this code what does that mean ?
danpost danpost

2014/4/24

#
What does your 'getcueBall method look like in your PoolTable class?
bizzard bizzard

2014/4/25

#
okay i got that part.. but got another issue i am making my ball bounce every time when it hits the wall but its keep bouncing and doesn't stop. how do i make that work .. here is my code if(currentY<=0 || currentY > getWorld().getHeight()-1 ) { ySpeed = -ySpeed; } if(currentX<=0 || currentX > getWorld().getWidth()-1 ) { xSpeed = -xSpeed; }
danpost danpost

2014/4/25

#
You need to check both the direction and edge for each bounce (if moving right and hitting right wall, change direction).
bizzard bizzard

2014/4/25

#
it goes left right, top bottom every direction but just doesn't stop. it suppose to act exactly like how we play PoolGame.. hit bounce and stop.. mine is hitting and keep bouncing and never stop
danpost danpost

2014/4/25

#
You need to add drag (friction between the cloth and the balls) to the Ball class. As long as they have speed, subtract a small percentage of it each act cycle. Along with that, you could also take a percentage of it off when bouncing off the sides.
bizzard bizzard

2014/4/25

#
how do i do that can you give me a similar example? please!!
danpost danpost

2014/4/25

#
It would depend too much on how your Ball class is coded for there to be much of a chance for an example to be of any help.
There are more replies on the next page.
1
2