Page 1 of 2 12 LastLast
Results 1 to 20 of 31
Like Tree2Likes

Thread: Combine .setTranslateY with 'if'

  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Combine .setTranslateY with 'if'

    Im developing basic game. I want to make some events...

    character.setTranslateY (50);

    Java FX Code:
    if ( character.TranslateY.equals.50; ) {
        	   right.setVisible (false);
            }
    The problem is that I dont know how to write condition properly. While setTranslateY sets coordinates, this format is not recognized for 'if condition'

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Combine .setTranslateY with 'if'

    if ( character.TranslateY.equals.50; ) {
    That statement shows a basic misunderstanding of how to write java code.
    There should not be a ; inside of the ()s
    You need to understand what symbols are for methods and which ones are for variables.
    For variables you need to know which ones are for objects and which ones are for primitives.

    You can not call a method using a primitive.
    A numeric literal like 50 can not be referenced: ... equals.50
    A method needs () with its args inside of the ()s
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Combine .setTranslateY with 'if'

    To write confidition with simple integers is no problem.

    int a = 10;

    Java FX Code:
    if (a<12){
    a++;
    }
    But there comes confusing with .setTranslateY (50);
    There should be something like coordinate = 50;
    But I dont know. And Im not familar how to insert parameter value for condition.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Combine .setTranslateY with 'if'

    Have you read the API doc for the class with the setTranslateY method?

    There should be something like coordinate = 50;
    Why? Classes try to protect their variables by making them private and requiring a set method to change their contents. Directly assigning a value to a variable is not allowed. Most classes with set methods have get methods also.

    What are you trying to do? Can you explain in English?
    Last edited by Norm; 01-13-2017 at 02:30 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Combine .setTranslateY with 'if'

    Can I suggest not doing GUI stuff at the moment?
    Write a simple text game using the command line, so you can get used to how Java methods work.
    GUI frameworks require you to understand how Java works, and so are not something a beginner should be using.
    benji2505 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,225
    Rep Power
    13

    Default Re: Combine .setTranslateY with 'if'

    Quote Originally Posted by Norm View Post
    Have you read the API doc for the class with the setTranslateY method?
    Are you kidding?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Combine .setTranslateY with 'if'

    Quote Originally Posted by Norm View Post
    Have you read the API doc for the class with the setTranslateY method?


    Why? Classes try to protect their variables by making them private and requiring a set method to change their contents. Directly assigning a value to a variable is not allowed. Most classes with set methods have get methods also.

    What are you trying to do? Can you explain in English?
    https://www.youtube.com/watch?v=GcAHXD68THw

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Combine .setTranslateY with 'if'

    Sorry, I don't like using links. Post any response you have here.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Combine .setTranslateY with 'if'


  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Combine .setTranslateY with 'if'

    Please respond to the questions in post#4 with text in this thread. Not images or videos.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Combine .setTranslateY with 'if'

    One button have to become invisible, once it reach certain coordinates.

    Once image reach those coordinates.

    Java FX Code:
    character.setTranslateY (50); 			
      character.setTranslateX (0);
    I want to apply action.
    But I dont know how to use "if ( )" condition this time.

    Because 'character.setTranslateY (50)' should be used as condition, but compilator dont likes it.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Combine .setTranslateY with 'if'

    compilator dont likes it.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Also show the definitions for the variables used in the statement so we know what data type they are.

    'character.setTranslateY (50)' should be used as condition
    What does the API doc say about the setTranslateY() method? What type of value does it return that could be used in an if statement?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Combine .setTranslateY with 'if'

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Also show the definitions for the variables used in the statement so we know what data type they are.
    Type missmatch: cannot convert from void to boolean.
    Java FX Code:
    Image charak= new Image (new FileInputStream("head.jpg") );
    ImageView character = new ImageView (charak);
    character.setTranslateY (0); 			
    character.setTranslateX (0);

    What does the API doc say about the setTranslateY() method? What type of value does it return that could be used in an if statement?
    Translate (JavaFX 8)
    I dont understand here. Docs even dont bother to show simple example.
    Last edited by asdfg; 01-13-2017 at 08:09 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Combine .setTranslateY with 'if'

    Type missmatch: cannot convert from void to boolean.
    That says the expression that was used in the statement returned void, but a boolean value was required.
    An if statement requires a boolean value.
    What values do you want to compare with an if statement?


    Java FX Code:
    ImageView character = new ImageView (charak);
    The variable character is of type ImageView. You need to read the API doc for the ImageView class.
    What does the API doc say about the setTranslateY() method? What type of value does it return that could be used in an if statement?
    Is there another method that returns a value that could be tested?

    Why is there a link for the Translate class in your post?
    Last edited by Norm; 01-13-2017 at 08:20 PM.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,225
    Rep Power
    13

    Default Re: Combine .setTranslateY with 'if'

    In the first code example I see nothing which would give that error.

    In the first code example or document link I see no setTranslateX or setTranslateY methods.
    Edit: The above statement is incorrect. I found the methods in the Node class.

    And if the JavaDoc showed examples of everything it would be as big as the unabridged OED.

    Regards,
    Jim
    Last edited by jim829; 01-14-2017 at 12:06 AM. Reason: Correction
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Combine .setTranslateY with 'if'

    Also posted at: https://coderanch.com/t/674837/java/setTranslateY

    Strange the OP couldn't ask the question like it was asked there.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,225
    Rep Power
    13

    Default Re: Combine .setTranslateY with 'if'

    Ok. My earlier reply was incorrect. ImageView inherits from Node and Node has the get and set translate methods. I missed it the first time I was reading the java doc. However, set returns void (i.e. nothing) and get returns a double. So you can't use them as boolean values. So you would need to use them like this:

    Java FX Code:
    if (character.getTranslateX() == 50) {
       // then do something.
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Combine .setTranslateY with 'if'

    Do I really have formatted question so unclear? I even made a short-video.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Combine .setTranslateY with 'if'

    This was left off the questions on this forum:
    I want to write a code:

    if character coordinates is Y=50 and X=0,
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Combine .setTranslateY with 'if'

    So, our friends at Coderanch gave away the solution - too bad . Norm pretty much gave you the solution in post #4 already. As a night read you should check "Encapsulation" as well as "Getters and Setters". It is one of the fundamental concepts in OOP. JavaFX follows this concept very closely.

    I do think that the proposed solution at coderanch is incorrect in the sense of the game though. I would think that you disable the down button for all cells on the lower line and generally disable the respective buttons on the outside lines.

    So, did you figure out how to set the Button instances disabled?
    Norm likes this.

Page 1 of 2 12 LastLast

Similar Threads

  1. Combine Error
    By cloud in forum Eclipse
    Replies: 1
    Last Post: 12-07-2011, 09:26 AM
  2. Combine two S.O.P in one
    By javauserjava in forum New To Java
    Replies: 5
    Last Post: 04-05-2011, 06:27 AM
  3. can i combine this 2 code into one?
    By reeveliew in forum New To Java
    Replies: 3
    Last Post: 05-09-2010, 02:24 PM
  4. How can I do this? Combine variable.
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 01-14-2009, 05:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •