Danielx Anderssonx

Greenhorn
+ Follow
since Nov 18, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
14
Given in last 30 days
14
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Danielx Anderssonx

I solved it by writing:
But the string of text i had, "Lives left" had to go since the string did not like to share the line of code with .StringValueoOf().
Its not a big problem i could just write a new g.drawString and adjust the coordinates.
2 days ago
I got an integer that stores the x life left of the game.


How can I change this code:

So it also shows the current integer value of  the variable "lives" ?
2 days ago
I meant to say: How could i rewrite a new class that uses rectangles instead of sprites ?
1 week ago
I got a superclass "Entity" that gives sprites to it's sub-classes. But can I somehow use polymorphism to create a new entity "Wall" that uses rectangles instead of sprites to create it's objects?
Looking at the class ShipEntity.java how could i rewrite a new classes that uses rectangles instead of walls? Hope that makes sense, thanks for advice.


Entity.java


ShipEntity.java
1 week ago

Junilu Lacar wrote:There is fundamentally broken with code that has to resort to things like this:

The whole point of defining a hierarchy of Entity classes is so that you can use the type system and polymorphism to dispatch to the appropriate method. Using the instanceof operator in this case circumvents that mechanism. It's kludgy and could probably be done differently and more elegantly.


Thanks for the link about polymorphism.

Any ides how to get this to work? Im stuck. My thinking is that maybe i could define 4 new rectangles from the bat somehow. Then declare different movement behaviors for each rectangle.
1 week ago
I'm trying to write a breakout game where the ball behaves differently depending on where the ball hits the bat.
I don't know how to define different behaviors depending on where the collusion occurs with the same object.
My code is copied from a Space Invader tutorial so the names don't make much sense.





The relevant source code, I think, 2 classes from the project posted down below. Also the whole project with sprites can be downloaded here: https://ufile.io/33762
Im stuck All feedback is good

Entity.java


ShotEntity.java

1 week ago

Norm Radder wrote:Did you read the tutorial at the link I provided?

Where does your code add a mouse listener to a GUI object?  A GUI object with the focus will call any listeners that have been added to it.

Can you post the GUI code where you have added the mouse listener?


** I meant to say that I dont have a code for that. I tried to take some code from this example:

1 week ago

Norm Radder wrote:Did you read the tutorial at the link I provided?

Where does your code add a mouse listener to a GUI object?  A GUI object with the focus will call any listeners that have been added to it.

Can you post the GUI code where you have added the mouse listener?


Hmm I have a code for that. I guess the GUI in my case would be: or
Yeah I maybe have to read some more if it's to complicated to explain. Thanks for the link.
1 week ago
I tried to read some of the MouseListenerExamples but I don't really get it to work in my program.
The code is still basically the same but I added a MouseListener class. Would be really awesome if someone could take a few minutes and give me some feedback on what I did wrong. :/

BatMouseListener.java
1 week ago

Norm Radder wrote:

What does that mean?



Take a look at the tutorial: https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html


Thanks (y)
2 weeks ago

Norm Radder wrote:Where is the code that sets the listener?


What does that mean?

Campbell Ritchie wrote:oving to our GUIs forum

Don't use a mouse motion listener. Your rectangle will follow the mouse wherever it goes, until you leave the area of your GUI. Unless you implement the method to put the rectangle at (0, 0), which will always move it to the top left.
Use a mouse listener and implement its clicked method to attract the rectangle to your click. If you simply use the coordinates of the click (obtainable from the mouse event object), that will attract the top left corner of your rectangle to your click. Use a mouse motion adapter or a mouse adapter and make sure to use the @Override annotation.
Why have you got a mouse moved method in the bat class? I don't think that is right.


Well I only wanted to move to a specific location to start with, to get something to happen.
But later on I intended to make it so the rectangle followed mouse wherever it goes (in x).
Yeah I might make a seperate move class later on.
2 weeks ago
I want a rectangle to move to a specific location when the mouse is moved. But my code don't respond to the movement of the mouse.
The line of code i am referring to is:
Any ideas on how to get it working?


Main.java

Bat.java
2 weeks ago

Campbell Ritchie wrote:


i figured it out.


2 weeks ago

Campbell Ritchie wrote:




When defining the text with:
How can i change the code below to respond to that?
2 weeks ago

Campbell Ritchie wrote:Maybe you will have to pass the speech bubble text to a constructor or setXXX() method.


I'm going to pass the speech bubble to a constructor. But could you show me how that code would look?
2 weeks ago