Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Using Modulus to alternate between 2 functions?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Using Modulus to alternate between 2 functions?

    Hello.
    This code is for a game I'm making.
    I'm new to using modulus, and I've been looking all over the place for a tutorial that I can understand. No luck. :\

    What I'm trying to do is get the console to print "enemy hits player" as soon as enemyAttacksPlayer() is called.
    After one second, "player hits enemy" should be printed, so it alternates between these two lines every second.

    The issue is when I call enemyAttacksPlayer(), it takes a few seconds to print any lines, and this delay is kinda annoying.

    After messing with the numbers for about 30 minutes, I gave up and came here.

    public int time = 0;
     
    	public void enemyAttacksPlayer() {
    		time++;
    		if (time % 120 == 60) {
    			System.out.println("enemy hits player");
    		}
    		if (time % 120 == 0) {
    			System.out.println("player hits enemy");
    		}
    	}
    I think I may be using % wrong. Any help?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using Modulus to alternate between 2 functions?

    I believe you're asking for help with eliminating or reducing the delay, and the delay is not occurring in the code you posted. Show your game loop, or the while ( true ) loop, that keeps the game going.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using Modulus to alternate between 2 functions?

    How often is enemyAttacksPlayer() called? It's coded to do something with 2 times out of 120 times it is called.
    What is it supposed to do the other 118 times it is called per 120 calls.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using Modulus to alternate between 2 functions?

    Ah, now you guys can tell how new I am to this.
    I didn't even provide enough information to get the help that I needed.

    enemyAttacksPlayer() is called 60 times per second when I'm close enough to an enemy.

    My game was updating 60 times per second, so I got rid of the % and used some simple if (time == 1) and if (time == 61) statements.
    I also looped time so it resets to 0 after 119
    I would've used if (time == 0) and if (time == 60) but for some reason it would call the time == 60 one second after calling the main method,
    so I guess time had to be 1 before it could be called correctly. 1/60 of a second is really that bad of a delay, so I might stick with this.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using Modulus to alternate between 2 functions?

    There are excellent discussions of game loops, animation timing, etc. on the Internet. I can't tell what you're doing exactly, but I'm sure it can be improved. Search for the topics I've suggested and read what others have to say, try what they suggest, and learn new (maybe better) ways to accomplish what you're trying to do.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using Modulus to alternate between 2 functions?

    After looking up how to properly increment and loop a value, I got it to work without any delay at all.
    Thanks.

Similar Threads

  1. modulus question
    By meangreen2003 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 18th, 2014, 05:19 PM
  2. Alternate Decision Process Needed
    By brooksr13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 12th, 2013, 04:27 AM
  3. BruceForce Method and Modulus HELP!
    By JAVAHELPP in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 13th, 2013, 03:38 AM
  4. Modulus with BigIntegers
    By JDreben in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2012, 10:00 PM
  5. Java Game - Alternate between players
    By JohnQ in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 21st, 2011, 08:49 PM

Tags for this Thread