• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BrianJensen

[Java][?] ActionListener & Timer

7 posts in this topic

ActionListener listener = new ActionListener()
        {
            @Override public void actionPerformed(ActionEvent e)
            {
                boolean isfin = false;
                do {
                    NSplitFrame += NSplitFrameInc;
                    if (NSplitFrame < 4 || NSplitFrame > 12)
                    {
                        NSplitFrame = 8;
                        NSplitFrameInc = -NSplitFrameInc;
                    }
                }while(isfin);
            }
        };
        displayTimer = new Timer(200, listener);
        displayTimer.start();

with or without the do :: while every once in a while I get a ArrayOutOfBoundsException due to NSplitFrame being > 12. Is there a way to make sure that this listener has completely finished while the values are being changed?

Edited by 0Circle0
0

Share this post


Link to post
Share on other sites

First and foremost, because you declare isfin as false and then go straight into your loop, the body of your loop will only run once every time, so you really have no need for your loop. Secondly,  within your do-while, you need a statement that can change the value of your boolean isfin. Otherwise, if isfin is true, your program will loop endlessly, even if NSplitFrame is past your limits.

Now I don't know what you're using NSplitFrame for, but there are multiple ways of preventing your ArrayOutOfBoundsException. In most cases, if you're iterating through an array, it is recommended that you use a loop - usually a for loop. However, any loop can be written as any other loop (for, while, do-while).
 

If you can clarify what you're trying to do with your loop, I can be of a bit more help.

Edited by epicpunnum
0

Share this post


Link to post
Share on other sites

What is the size of your array? A 12 element array has valid indices 0..11. Also, it appears Timer tasks are run in a separate thread, and you don't appear to have any synchronisation on the values accessed and modified by this thread.

0

Share this post


Link to post
Share on other sites
        if (NSplitFrame == 8)
                        NSplitFrame += NSplitFrameInc;
                    else
                    {
                        NSplitFrame = 8;
                        NSplitFrameInc = -NSplitFrameInc;
                    }

I changed it to this and so far it is good. How would I go about synchronization the actionListener and the main thread?

 

The value can only = 4, 8 or 12. But before the program might every once in a while = 0 or 16

Edited by 0Circle0
0

Share this post


Link to post
Share on other sites

Hum, first, you should decorrelate the frame index and the actual location of the frame in your array, thus having 

 

0<=NSplitFrame <AnimationSize
 

 

. Then, the real frame is

 

RealSplitFrame = SplitFrameBase + NSplitFrame
 

 

 

Now I don't get your code, I mean it is an overcomplicated way to obtain either 8+NSplitFrameInc or 8-NSplitFrameInc.

0

Share this post


Link to post
Share on other sites

How would I go about synchronization the actionListener and the main thread?

One way is to ensure all access to the variables in question is in synchronized methods/blocks (provided you sychronize on the same object).

 

Here is a simple program that, at least on my system, illustrates the surprising effects that lacking synchronisation can have:

 
public class Example
{
    private int x;
    
    private int y;
    

    public /* * / synchronized / * */ int access() {
        return x - y;
    }
    
    public /* * / synchronized / * */ void modify() {
        ++x;
        ++y;
    }

    
    public static void main(String[] args) {
        final Example example = new Example();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    example.modify();
                }
            }
        });
        
        thread.start();
        
        try {
            for(int i = 0 ; i < 10 ; ++i) {
                int difference = example.access();
                System.out.println("Difference: " + difference);
                
                try {
                    Thread.sleep(1000);
                } catch(InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        } finally {
            System.exit(0);
        }
    }
}

One might naively expect that the two values will always be the same, so the difference should always be 0. A slightly less naive programmer might thing that the difference will always be 0 or 1, if the modifying thread is "between" incrementing X and Y.

 

Unfortunately, reality is much more complex. Without synchronisation, there are very few assumptions one can make about the output of this program. Without the synchronized blocks, here is sample output I got from running this program:

 

Difference: 0
Difference: -30
Difference: -12
Difference: -49
Difference: -60
Difference: -66
Difference: -37
Difference: -42
Difference: 0
Difference: -10

Quite surprising behaviour! This is known as a "race condition". Note the scary part: the result sometimes appears to be correct. It is possible to write code that looks like it works (under certain timing conditions), but may fail when those conditions change (like, when you release your application to the general public with faster or slower computers that you developed on).

 

However, removing the comments on both methods (thus ensuring that all access is correctly synchronized), gives us the "intended" output:

 

Difference: 0
Difference: 0
Difference: 0
Difference: 0
Difference: 0
Difference: 0
Difference: 0
Difference: 0
Difference: 0
Difference: 0

 

There are other ways to synchronize programs.

 

That said, it appears the first result I found via Google where the general "Java Timer" class, not the "Swing Timer". The latter runs on the "event dispatch thread", so it is possible that, depending on where your other accesses occur, all accesses are performed on the same thread. You should carefully check that this is the case - if so, you do not need to synchronize your code.

0

Share this post


Link to post
Share on other sites

Some really good info here. Little above me but I'll study rip-off's post for a bit.

ActionListener listener = new ActionListener()
        {
            @Override public synchronized void actionPerformed(ActionEvent e)
            {
                if (NSplitFrame == 8)
                    NSplitFrame += NSplitFrameInc;
                else
                {
                    NSplitFrame = 8;
                    NSplitFrameInc = -NSplitFrameInc;
                }
            }
        };
        displayTimer = new Timer(200, listener);
        displayTimer.start();

then made the function that uses NSplitFrame 

@Override public synchronized void run() //Runable
{
        ...code here
}

Am I doing that right?

0

Share this post


Link to post
Share on other sites
That won't do. Adding the synchronized keyword to a (non-static) method is basically just syntactic sugar for wrapping everything inside the method into a "synchronized(this) { }" block. However, the ActionListener is another instance and no synchronization at all will happen. You could probably use something like
@Override public void actionPerformed(ActionEvent e)
{
   synchronized(OutsideClassName.this)
   {
      // what you did before
   }
}
where OutSideClassName has to be replaced by the class in which you define the action listener and with which you need synchronization. A probably better alternative would be to not do anything specific in the action listener and instead call a (synchronized) method of that class to do the work.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0