Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2
1  Game Development / Newbie & Debugging Questions / Dynamic hair on: 2013-12-08 07:47:58
Hey guys!
My character shall have a ponytail that reacts to wind, gravity etc. separately from the character.

I tried and easy verlet integration but it doesn't well.

Left how I want it to be,
right how it is with my verlet integration.

Any Ideas how I can solve this?

This is my hair Class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Entity;

import java.awt.Graphics2D;
import java.awt.Stroke;

/**
 *
 * @author Nils
 */

public class Hair extends Entity
{

  private HairJoint[] hairJoints;
  private HairMesh[] hairMeshes;
  private int cols;
  private int rows;
  private double w;

  public Hair(int rows, int cols, double x, double y, double weight)
  {
    this.w = weight;
    this.x = x;
    this.y = y;
    this.cols = cols;
    this.rows = rows;
    hairJoints = new HairJoint[rows * cols];
    hairMeshes = new HairMesh[(cols - 1) * rows + (rows - 1) * cols];
    int i = 0;
    for (int r = 0; r < rows; r++)
    {
      for (int c = 0; c < cols; c++)
      {
        hairJoints[r * cols + c] = new HairJoint(x, y+=rows*rows*.5);
        if (c > 0)
        {
          hairMeshes[i++] = new HairMesh(hairJoints[r * cols + c - 1],
                  hairJoints[r * cols + c]);
        }
        if (r > 0)
        {
          hairMeshes[i++] = new HairMesh(hairJoints[r * cols + c], hairJoints[(r
                  - 1) * cols + c]);
        }
      }
    }
   
  }

  public void setPosition(double x, double y)
  {
    hairJoints[0].x = x;
    hairJoints[0].y = y;
  }

  @Override
  public void render(Graphics2D g)
  {
    for (int i = 0; i < hairMeshes.length; i++)
    {
      hairMeshes[i].render(g);
    }
  }

  @Override
  public void process()
  {
    x += velX;
    y += velY;
    int t = hairJoints.length;
    int i;
    hairJoints[0].setPos(x, y);
    double weight = w;
    for (i = cols;i < t;i++)
    {
      hairJoints[i].y+= w/= 1.81;
      hairJoints[i].process();
    }
    t = hairMeshes.length;
    for(int stiff = 0;stiff < 6;stiff++)
    {
      for (i = 0;i < t;i++)
      {
        hairMeshes[i].process();
      }
    }
  }

  private class HairJoint extends Entity
  {

    private double x;
    private double y;
    private double oldX;
    private double oldY;

    public HairJoint(double x, double y)
    {
      setPos(x, y);
    }

    public void setPos(double x, double y)
    {
      this.x = oldX = x;
      this.y = oldY = y;
    }

    @Override
    public void render(Graphics2D g)
    {
    }

    @Override
    public void process()
    {
      double tempX = x;
      double tempY = y;
      x += x - oldX;
      y += y - oldY;
      oldX = tempX;
      oldY = tempY;
    }
  }

  private class HairMesh extends Entity
  {

    private HairJoint jointA;
    private HairJoint jointB;
    private double hypotenuse;

    public HairMesh(HairJoint a, HairJoint b)
    {
      jointA = a;
      jointB = b;
      double dx = a.x - b.x;
      double dy = a.y - b.y;
      hypotenuse = Math.sqrt(dx * dx + dy * dy);
    }

    @Override
    public void render(Graphics2D g)
    {
      g.drawLine((int) (jointA.x), (int) (jointA.y), (int) (jointB.x),
              (int) (jointB.y));
    }

    @Override
    public void process()
    {
      double dx = jointB.x - jointA.x;
      double dy = jointB.y - jointA.y;
      double h = Math.sqrt(dx * dx + dy * dy);
      double diff = hypotenuse - h;
      double offX = (diff * dx / h) *.2;
      double offY = (diff * dy / h)* .2;
      jointA.x -= offX;
      jointA.y -= offY;
      jointB.x += offX;
      jointB.y += offY;
    }
  }
}
2  Game Development / Newbie & Debugging Questions / Re: 2D JnR Lighting with Java2D on: 2013-11-30 13:32:46
Well, I know that, the thing is I don't know how.
I only find tutorials "how to colorize a grayscale bufferedImage" etc. not how to manipulate bufferedImages with colors...
3  Game Development / Newbie & Debugging Questions / 2D JnR Lighting with Java2D on: 2013-11-30 13:12:30
Hey there!

I want to implement a (not-so-dynamic) lightengine into my game.
But I don't know how.
My Map is a simple int[][] and Tiles drawn are 16x16.

I somehow thought of a class LightEmitter, which produces light.
Every Tile has some kind of "Light Damping" which reduces the light by a certain value.
e.g. you have a light with an intensity of 16 and the tiles around it have a damping value of 2. This means the light doesn't reach any other tiles after 8.
My Tilemap should also use a "shadow color". Light-Value 0 is the shadow color so it doesn't appear completely black without light.

Any Ideas How I can manipulate the appearance of the tiles? I want to implement 2 Methods:
"Hard" Lighting and Smooth Lighting
4  Java Game APIs & Engines / Java 2D / Re: Resource Loader / Loading Screen on: 2013-11-23 19:27:45
I implemented a File e.g. into my TitleFolder.
It describes the TitleScreen:
how many BGs,
how many maps(only used in Levels, but for standardization) are used
The Sound Files are also stored in a separated file, the same with the Music.

For every File described in the "Description" File, the progressbar gets a "MaxValue++".
For every loaded File the Progressbar gets a "Fill++".
It now looks like this:


Thanks for your help :]
5  Java Game APIs & Engines / Java 2D / Resource Loader / Loading Screen on: 2013-11-23 16:43:59
Hey there!
I want to make a Loading Screen( a progress bar painted with Java2D)

Painting the ProgressBar is no problem.
I'm more confused how to load the Resources dynamically.

My game is seperated by Scenes and the Game should be like this:
  • Loading
  • Title Screen
  • Loading
  • Level1
  • Loading
  • Level2
and so on. Since Title and every Level uses different Fields and Images and Music and Sounds to load, I have no Idea how to start a "SceneLoader".
I have no Idea how to "detect" what has to be loaded because for example Level 1 uses 2 Backgrounds, while Level 2 uses 4 Backgrounds etc. Clueless
6  Game Development / Newbie & Debugging Questions / Re: Playing already stored Clip File on: 2013-11-22 15:06:01
Hey, thanks for the answer, but I found another solution since resetting the framePosition doesn't allow to play the same sound multiple times at the same time.

I converted my File into a byte[] and put it into the Object SoundEffect.
When invoking the playSound() of SoundEffect, the method opens a new AudioInputStream.
7  Game Development / Newbie & Debugging Questions / Re: Playing already stored Clip File on: 2013-11-21 20:28:13
okay thanks.
But I can play these sounds only once.
How do I reset the AudioInputStream from a Clip?
Since they're all loaded into my LinkedList after clip.open(ais), I have no Idea how I make them reusable
8  Game Development / Newbie & Debugging Questions / Re: Playing already stored Clip File on: 2013-11-20 21:08:57
Is this okay? I want to put a lot of sounds into my LinkedList and I'm afraid, that this will eat a lot of RAM...
9  Game Development / Newbie & Debugging Questions / Re: Playing already stored Clip File on: 2013-11-20 20:03:20
I read an audio file, and store it in a linkedList
1  
2  
3  
4  
5  
            AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(
                    "/Resources/Audio/Samples/" + st.nextToken()));
            Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class,
                    ais.getFormat()));
            sounds.get(soundName).add(clip);


when passing the Clip to my new Player-Class, it doesn't play with "clip.start()".
10  Game Development / Newbie & Debugging Questions / Playing already stored Clip File on: 2013-11-20 19:33:29
Hey there!
I want to put sounds into my Game.

I already stored some Clip Objects and I want to play them with an external Class.

AudioInputStream wants to have an InputStream, but my Clips are already in the memory, how do I play them then?  Clueless
11  Game Development / Newbie & Debugging Questions / Collision Detection on: 2013-07-23 10:07:32
Hey there.
I'm currently thinking of an algorithm for a collision detection.
I want to implement various tile-collisions...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
  public static final byte COLL_THROUGH = 0;
  public static final byte COLL_SOLID = 1;
  public static final byte COLL_SLOPE_R = 2;
  public static final byte COLL_SLOPE_L = 3;
  public static final byte COLL_12SLOPE_R = 4;
  public static final byte COLL_22SLOPE_R = 5;
  public static final byte COLL_12SLOPE_L = 6;
  public static final byte COLL_22SLOPE_L = 7;
  public static final byte COLL_14SLOPE_R = 8;
  public static final byte COLL_24SLOPE_R = 9;
  public static final byte COLL_34SLOPE_R = 10;
  public static final byte COLL_44SLOPE_R = 11;
  public static final byte COLL_14SLOPE_L = 12;
  public static final byte COLL_24SLOPE_L = 13;
  public static final byte COLL_34SLOPE_L = 14;
  public static final byte COLL_44SLOPE_L = 15;
  public static final byte COLL_STAIR_R = 16;
  public static final byte COLL_STAIR_L = 17;
  public static final byte COLL_14 = 18;
  public static final byte COLL_24 = 19;
  public static final byte COLL_34 = 20;
  public static final byte COLL_WATER_SURFACE = 21;
  public static final byte COLL_UNDERWATER = 22;

Here for a graphical visualisation:


Does someone have an Idea how I should start with this?
I've never done something like that before...
12  Game Development / Game Play & Game Design / Re: Need (non-paid)pixel-artist for game art on: 2013-07-10 14:17:14
I'd also like to see some progress.
If your game will be canceled, all the work is useless.
13  Game Development / Newbie & Debugging Questions / Trigger Cocept on: 2013-07-03 10:06:44
Hey there.I want to implement a trigger-system into my game.
I thought of something like that:

Enemies, Traps, Doors, Music, etc. implement an interface namend "Triggerable"
A switch, keyhole, or a simple area within the level, has an Attribute called "whatToTrigger" and is an ArrayList<Triggerable>.

But I have no Idea how to put the triggerable Objects into the Trigger of the Swtich/Keyhole/whatever.
Any ideas for that?
14  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 20:08:30
yeah, thanks alot to you guys :]]
15  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 19:55:48
I guess not.
My render-method looks like above:
1  
2  
3  
4  
5  
public void render()
  {
    gameScreenManager.render(buffer.createGraphics());
    g.drawImage(buffer, 0, 0, OptionsManager.currentDimension.width, OptionsManager.currentDimension.height,null);
  }



Works now, I created a new Graphics-Object.
16  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 19:35:37
Oh, that works. Huh
I should read javadoc first when using methods Roll Eyes

Thanks to you guys :]

Just another question:

before I used a bufferedImage, I used RenderingHints.
But now with the bufferedImage, they don't work.
Some workaround to make them work again?
17  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 19:31:00
The Panel and Frame sizes are all correct...
18  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 19:21:57
So the image stays the same size within the frame, even when you resize the frame?
You might (probably) need to call
1  
2  
g.dispose();
g = (Graphics2D) getGraphics();

whenever the frame changes size to "refresh" the graphics object.
The image is still small, when changing screensizes :/
19  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 19:05:51
Ok, it now shows everything as normal.

I have changed the line to this:
1  
g.drawImage(buffer, 0, 0, OptionsManager.currentDimension.width, OptionsManager.currentDimension.height, 0, 0, getWidth(), getHeight(), null);


this is my OptionsManager-Class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
package Core;

import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.Toolkit;

public class OptionsManager
{

  public static int volume = 100;
  public static int graphics = 0;
  public static boolean targetting = true;
  public static boolean fullscreen = false;
  public static Dimension currentDimension = new Dimension(960, 540);
  public static Dimension fullscreenDimension = Toolkit.getDefaultToolkit().getScreenSize();
  public static Dimension windowDimension = new Dimension(960, 540);

  public OptionsManager()
  {
  }

  public static void toggleFullscreen()
  {
    if (fullscreen == false)
    {
      GameCore.frame.setSize(fullscreenDimension);
      OptionsManager.currentDimension = fullscreenDimension;
      fullscreen = true;
      System.out.println(fullscreen);
    }
    else if (fullscreen == true)
    {
      GameCore.frame.setSize(windowDimension);
      OptionsManager.currentDimension = windowDimension;
      fullscreen = false;
      System.out.println(fullscreen);
    }
  }
}


but it still doesn't scale the Image :/
Only the frame changes it's size.
20  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 18:40:31
Well, I'm not using the paintComponent() Method.
I always send the Graphics object as parameter to the desired Screen and then to the object that has to be drawn.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
public void run()
  {
    initialize();

    while (isRunning)
    {
      beginTime = System.currentTimeMillis();
      framesSkipped = 0;   // resetting the frames skipped
     update();
      render();
      // calculate how long did the cycle take
     timeDiff = System.currentTimeMillis() - beginTime;
      // calculate sleep time
     sleepTime = (int) (FRAME_PERIOD - timeDiff);
      if (sleepTime > 0)
      {
        // if sleepTime > 0 we're OK
       try
        {
          // send the thread to sleep for a short period
         // very useful for battery saving
         Thread.sleep(sleepTime);
        }
        catch (InterruptedException e)
        {
        }
      }
      while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS)
      {
        // we need to catch up
       // update without rendering
       update();
        // add frame period to check if in next frame
       sleepTime += FRAME_PERIOD;
        framesSkipped++;
      }
    }
    setVisible(false);
  }
 

public void render()
  {
    gameScreenManager.render(g);
  }

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
package Core.GameState;

import Core.GameCore;
import Game.Screens.TitleScreen;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class GameScreenManager {
   
   public ArrayList<GameScreen> gameScreens;
   public static int currentState;
   public GameScreen screen;
   public static final int SCREEN_TITLE = 0;
   public static final int SCREEN_LEVEL = 1;
   
   public GameScreenManager() {
     
      gameScreens = new ArrayList<GameScreen>();
      currentState = SCREEN_TITLE;
    screen = new TitleScreen(this);
      gameScreens.add(screen);
   }
   
   public void setState(int state) {
      currentState = state;
      gameScreens.get(currentState).init();
   }
   
   public void update() {
      gameScreens.get(currentState).update();
   }
   
   public void render(Graphics2D g) {
      gameScreens.get(currentState).render(g);
   }
   
   public void keyPressed(int k) {
      gameScreens.get(currentState).keyPressed(k);
   }
   
   public void keyReleased(int k) {
      gameScreens.get(currentState).keyReleased(k);
   }
   
}



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Game.Screens;

import Core.GameCore;
import Core.GameState.GameScreen;
import Core.GameState.GameScreenManager;
import Core.OptionsManager;
import Graphics.ScreenObject;
import Graphics.UserInterface.VolumeBar;
import Sound.BackgroundMusic;
import Sound.SoundLibrary;
import World.Layer1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

/**
 *
 * @author Nils
 */

public class TitleScreen extends GameScreen
{
  //music

  BackgroundMusic titleMusic;
  String musicFile = "/Music/Title.mp3";
  Layer1 bg;
  BufferedImage bgImage;
  ScreenObject title;
  ScreenObject lara;
  Color selected;
  Color deselected;
  int currentScreen;
  String[] options = new String[]
  {
    "Start", "Load", "Options", "Quit"
  };
  private int currentChoice;
  private VolumeBar vb;

  public TitleScreen(GameScreenManager manager)
  {
    this.gameStateManager = manager;
    init();
  }

  @Override
  public void init()
  {
    GameCore.options.volume = 100;
    GameCore.options.targetting = true;
    GameCore.options.graphics = 0;
    GameCore.g.setRenderingHints(OptionsManager.graphicslow);
    soundlib = new SoundLibrary("Title");
    titleMusic = new BackgroundMusic(musicFile);
    titleMusic.start();
    selected = new Color(222, 217, 216);
    deselected = new Color(80, 69, 67);
    currentChoice = 0;
    currentScreen = 0;
    bg = new Layer1("/Graphics/Backgrounds/Cloud.jpg", 1);
    title = new ScreenObject("/Graphics/Pictures/TitleLogo.png");
    lara = new ScreenObject("/Graphics/Pictures/TitleLara.png");
    lara.setPosition(40, 100);
    title.setPosition((Core.GameCore.WIDTH / 2) - title.image.getWidth() / 2,
            100);
  }

  @Override
  public void update()
  {
    bg.setVector(-0.4, 0);
    bg.update();
    if (currentScreen == 0)
    {
      //Titlescreen
   }
    if (currentScreen == 1)
    {
    }
    if (currentScreen == 2)
    {
      //Options
     vb.update();
    }
  }

  @Override
  public void render(Graphics2D g)
  {
    bg.render(g);
  }

  @Override
  public void keyPressed(int k)
  {
[...]
  }

  @Override
  public void keyReleased(int k)
  {
  }
}


this is a chain, how Graphics g is used.

When I put buffer.drawImage();
right after
1  
gameScreens.get(currentState).render(g);

nothing is shown.

21  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 18:13:42
Well, I simple create an Graphics2D-Object with
Graphics2D g;
g = (Graphics2D) getGraphics();

22  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 17:56:17
yeah, scaling is the word I was searching for.

I'm not drawing onto a bufferedImage, how do I do that?
23  Java Game APIs & Engines / Java 2D / Re: Stretch Fullscreen? on: 2013-06-23 17:22:30
I don't want to make a normal fullscreen.
I want a STRETCHED fullscreen... hard to explain.
like this picture:

Widowed: like it is now.
Fullscreen(red) how it is, when I use fullscreen,
Coordinate 1920x1080 IS 1920x1080.
Stretched fullscreen (green) how I want it to be.
Coordinate 1920x1080 is now 960x540!
24  Java Game APIs & Engines / Java 2D / Stretch Fullscreen? on: 2013-06-23 16:56:08
Hey there!
My current JPanel/JFrame has a size of 960x540.
Is there a possiblity to stretch the whole Frame to fullscreen without having to recalculate imagesizes and positions?
25  Game Development / Newbie & Debugging Questions / Re: Animations with Java2D on: 2013-06-21 17:22:35
Thanks for your help.
But Somehow it doesn't want to draw the image.
Testwise I printed the (currentAnimation.getFrame())
it prints the 2 frames I inserted testwise (different sizes of the subimages)
but g.drawImage doesn't seem to work...


Works now, had a wrong BufferedImage file.
Thanks to you guys. Dunno what I'd do without ya  Grin
26  Game Development / Newbie & Debugging Questions / Re: Animations with Java2D on: 2013-06-21 16:15:10
I'm quite confused now Huh
In my initializing method at the start I have this (testwise):
1  
objectsToDraw.add(new Player(newImage));
(objectsToDraw is a ArrayList<Renderable>,Renderable is an interface)

my Player-Class looks like this now(testwise)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Core;

import Graphics.Animation;
import Graphics.Renderable;
import Graphics.Sprite;
import World.Actor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;

public class Player extends Sprite implements Renderable, Actor
{

  public static int health;
  public static final int maxHealth = 2000;

  /**
   * creates a Player-Object.
   *
   * @param img is the Spritesheet of the sprite representing the player.
   */

  public Player(BufferedImage img)
  {
    super(img);
    screenLocation = new Point(50, 50);
    initPlayer();
  }

  /**
   * Initiates the player for the Level. Means Animations will be added.
   */

  private void initPlayer()
  {
    BufferedImage tempImage = (BufferedImage) sheet;
    Animation temp = new Animation(sheet, true);
    tempImage.getSubimage(0, 0, 10, 5);
    temp.addFrame(tempImage, 5);
    tempImage.getSubimage(0, 5, 10, 5);
    temp.addFrame(tempImage, 5);
    animations.add(temp);
    System.out.println("initEnde");
  }

  /**
   * renders the current Player-Frame
   *
   * @param g
   */

  @Override
  public void render(Graphics g)
  {
  }

  /**
   * Disables the Render
   */

  @Override
  public void disableRender()
  {
  }

  @Override
  public void update()
  {
  }

  @Override
  public void calcBehav()
  {
  }

  @Override
  public void killThis()
  {
  }
}

my Animation Class looks like above(first post)

my Gameloop call these methods:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
private void update()
  {
    for (int j = 0; j < actorList.size(); j++)
    {
      actorList.get(j).calcBehav();
      actorList.get(j).update();
    }
  }

  public void render()
  {
    Graphics2D g = (Graphics2D) getGraphics();
    g.setBackground(new Color(0, 0, 0));
    for (int i = 0; i < layer1.size(); i++)
    {
      layer1.get(i).render(g);
    }
    for (int i = 0; i < layer2.size(); i++)
    {
      layer2.get(i).render(g);
    }
    for (int i = 0; i < screensToDraw.size(); i++)
    {
      screensToDraw.get(i).render(g);
    }
    for (int i = 0; i < objectsToDraw.size(); i++)
    {
      objectsToDraw.get(i).render(g);
    }
    for (int i = 0; i < layer3.size(); i++)
    {
      layer3.get(i).render(g);
    }
    for (int i = 0; i < userInterface.size(); i++)
    {
      userInterface.get(i).render(g);
    }

  }


I don't know how to play an animation.
It was just an idea.
27  Game Development / Newbie & Debugging Questions / Animations with Java2D on: 2013-06-21 15:19:20
Hey there, I'm currently working on a concept for animations for sprites.
At the moment I want to initialize my Player-Object which inherits following Attribute from Sprite:
1  
ArrayList<Animation> animations;

It's a Container for animations (accessing them via STATE Constants)
My Animation-Class is built like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
package Graphics;

import java.awt.Image;
import java.util.ArrayList;

/**
 *
 * @author Nils
 */

public class Animation
{

  private ArrayList<Frame> frames;
  private long totalTime, currentTime;
  private int frameIndex;
  private boolean looping;
  private Animation nextAnimation;
  private Image img;

  /**
   * Default constructor.
   */

  public Animation(Image img, boolean looping)
  {
    this.img = img;
    frames = new ArrayList<Frame>();
    this.looping = looping;
  }

  public void restart()
  {
    currentTime = frameIndex = 0;
  }

  /**
   * Adds an image to the animation.
   *
   * @param i The image to be added.
   * @param time The amount of time in milliseconds this image is displayed.
   */

  public void addFrame(Image i, long time)
  {
    totalTime += time;
    frames.add(new Frame(i, totalTime));
  }

  /**
   * Returns the current image displayed.
   *
   * @return The current image displayed.
   */

  public Image getFrame()
  {
    return frames.size() == 0 || isDone() ? null : frames.get(frameIndex).i;
  }

  public boolean isDone()
  {
    return looping ? false : frameIndex >= frames.size();
  }

  /**
   * Must be called to update the animation
   *
   * @param deltaTime The time passed since the last call to it.
   */

  public void update(long deltaTime)
  {
    if (frames.size() > 1)
    {
      currentTime += deltaTime;

      if (currentTime > totalTime)
      {
        if (looping)
        {
          frameIndex = 0;
          currentTime %= totalTime;
        }
        else
        {
          frameIndex++;
        }
      }

      if (!isDone())
      {
        while (currentTime > frames.get(frameIndex).time)
        {
          frameIndex++;
        }
      }
    }

  }

  public void setNextAnim(Animation anim)
  {
    this.nextAnimation = anim;
  }

  private static class Frame
  {

    private Image i;
    private long time;

    public Frame(Image i, long time)
    {
      this.i = i;
      this.time = time;
    }
  }
}


but how do I render my animations without a thread?
I somehow have to send the graphics-Object from Player, to the actual Animation and exchange the Player-Graphic with the one from the animation.
How do I do this?
28  Game Development / Newbie & Debugging Questions / Re: SoundLibrary with Hashtable on: 2013-06-20 16:35:40
Sry for double post Undecided

My idea was to read the files, convert them into byte[] and then put them into my Hashtable and playing them whenever I want.
(Because reading Files from the disk is too slow)
I created a new class called "SoundSample". It's constructor has the byte[] of the wav-File.
How do I convert my byte[] to a playable,storable sound?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
package Sound;

import java.io.*;
import java.util.StringTokenizer;

/**
 *
 * Reads a specific *.snd-File from the Level and reads its content
 * @author Nils
 */

public class LevelSoundFileReader
{

  BufferedReader br;
  String delims = ";";
  StringTokenizer st;
  String key = "";
  String file = "";
  String line;

  public LevelSoundFileReader(String levelname, SoundLibrary library)
  {
    try
    {
      BufferedReader br = new BufferedReader(new FileReader("Level/" + levelname
              + ".snd"));
      line = br.readLine();
      while (line != null)
      {
        st = new StringTokenizer(line, delims);
        while (st.hasMoreElements())
        {
          String key = st.nextToken();
          String file = st.nextToken();
          //convert the file into byte[]
         File soundFile = new File(file);
          FileInputStream fis = new FileInputStream(soundFile);
          byte[] bytes = new byte[(int)soundFile.length()];
          DataInputStream dis = new DataInputStream(fis);
          dis.readFully(bytes);
          //convert byteArray into a SoundSample
         SoundSample sound = new SoundSample(bytes);
          //library.put(key,bytes);
       }
        line = br.readLine();
      }
    }
    catch (IOException ex)
    {
      ex.printStackTrace();
    }
  }
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
package Sound;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.sound.sampled.Control.Type;
import javax.sound.sampled.*;

public class SoundSample
{

  Clip soundClip;

  /**
   * Constructor to create a new SoundSample out of byteArrays
   *
   * @param data
   */

  public SoundSample(byte[] data)
  {
    try
    {
      ByteArrayInputStream oInstream = new ByteArrayInputStream(data);
      AudioInputStream oAIS = AudioSystem.getAudioInputStream(oInstream);
      soundClip.open(oAIS);
    }
    catch (LineUnavailableException | UnsupportedAudioFileException | IOException ex)
    {
    }
  }

  public void play()
  {
  }
}

29  Game Development / Newbie & Debugging Questions / Re: SoundLibrary with Hashtable on: 2013-06-20 13:08:49
Yep, that was it.
Makes sense, the file is already a String when loaded into the application, so quotation marks are useless Cheesy
30  Game Development / Newbie & Debugging Questions / Re: SoundLibrary with Hashtable on: 2013-06-20 10:59:39
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
package Sound;

import java.util.HashMap;

/**
 *
 * @author Nils
 * Contains a levelspecific Soundmap
 */

public class SoundLibrary
{
  public HashMap<String,String> sounds;
  public SoundLibrary(String levelname)
  {
    sounds = new HashMap<String,String>();
    LevelSoundFileReader lsfr = new LevelSoundFileReader(levelname,this);
    System.out.println(sounds); //prints {"PISTOL"="Sound/pistol.wav", "FOOT"="Sound/ftstp.wav"}
   System.out.println(sounds.get("PISTOL")); //prints null
 }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
package Sound;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 *
 * Reads a specific *.snd-File from the Level and reads its content
 * @author Nils
 */

public class LevelSoundFileReader
{

  BufferedReader br;
  String delims = ";";
  StringTokenizer st;
  String key = "";
  String file = "";
  String line;

  public LevelSoundFileReader(String levelname, SoundLibrary library)
  {
    try
    {
      BufferedReader br = new BufferedReader(new FileReader("Level/" + levelname
              + ".snd"));
      line = br.readLine();
      while (line != null)
      {
        st = new StringTokenizer(line, delims);
        while (st.hasMoreElements())
        {
          String key = st.nextToken();
          String file = st.nextToken();
          library.sounds.put(key,file);
        }
        line = br.readLine();
      }
    }
    catch (IOException ex)
    {
      ex.printStackTrace();
    }
  }
}

Pages: [1] 2
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (9 views)
2014-10-02 14:36:20

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!