• What do you need help with? Version 5
    5,752 replies, posted
JAVA: [IMG]http://s13.postimage.org/x1mbwm513/d8b7cdced523d1ec02370c8e59d3fba2.png[/IMG] How would I go about making an instance of an object in a way so that it can be used by other methods in the same class?
[QUOTE=Cheezy;35833729]JAVA: [IMG]http://gyazo.com/d8b7cdced523d1ec02370c8e59d3fba2[/IMG] How would I go about making an instance of an object in a way so that it can be used by other methods in the same class?[/QUOTE] declare it outside a method
[QUOTE=swift and shift;35833745]declare it outside a method[/QUOTE] That'd be the best solution, but for some reason that just gives me a huge list of stack overflows when I run the program.
no it won't
[QUOTE=Mordi;35833499]I'm trying to use TinyThread for a loading-routine. I've already got the loading-function up, and I've made it so that when it returns true, the program assumes it's done loading, and moves on. However, there doesn't seem to be any proper tutorials on how to use it. There is only this -> [url]http://tinythread.sourceforge.net/?page=functionality[/url] It doesn't seem too complicated, but I'm not sure how to use it. Could anyone help?[/QUOTE] The given examples should be pretty self explanatory, maybe you should read up on how multithreading works. [url]http://www.codeproject.com/Articles/14746/Multithreading-Tutorial[/url] [url]http://en.wikipedia.org/wiki/Thread_safety[/url] [url]http://en.wikipedia.org/wiki/Mutual_exclusion[/url] [QUOTE=Cheezy;35833729]JAVA: How would I go about making an instance of an object in a way so that it can be used by other methods in the same class?[/QUOTE] My Java is a bit rusty but it should be something like this: [cpp]public class Example { private controller.Controller newController; public Example() { // Constructor newController = new controller.Controller(); } public method1() { // do stuff } public method2() { // do stuff } }[/cpp] You should also probably use the [b]this[/b] keyword to access member variables and objects.
[QUOTE=Chryseus;35833768] My Java is a bit rusty but it should be something like this: [cpp]public class Example { private controller.Controller newController; public Example() { // Constructor newController = new controller.Controller(); } public method1() { // do stuff } public method2() { // do stuff } }[/cpp] You should also probably use the [b]this[/b] keyword to access member variables and objects.[/QUOTE] Thanks a lot, that did the trick!
[QUOTE=Gmod4ever;35830191]Hey guys. I solved my compile issues (turns out you need a ";" after the "}" when declaring a class...), and I think I've managed to figure out how I want to make my manager object. [/QUOTE] God fuck I hate that mistake. It doesn't generate a normal "Error: expected a ;" or something but everything just stops working...
I have a 3D "dungeon" in XNA. The walls have individual BoundingBoxes around them. I also have a player, that is moved with velocityX and velocityZ. How can i make realistic collisions with having only one BoundingBox/Sphere around the player? This is how i move the player: [code]int velocityX = 0; int velocityZ = 0; if (ks.IsKeyDown(Keys.W)) { velocityZ = 1; } if (ks.IsKeyDown(Keys.S)) { velocityZ = -1; } if (ks.IsKeyDown(Keys.A)) { velocityX = 1; } if (ks.IsKeyDown(Keys.D)) { velocityX = -1; } cameraPos += (cameraRotation.Forward * currentSpeed * elapsedTime) * velocityZ; cameraPos += (cameraRotation.Left * currentSpeed * elapsedTime) * velocityX; velocityZ = 0; velocityX = 0;[/code] I am already generating the BoundingBoxes around the walls and i can access them very easily. I also have a function to test if a BoundingBox is colliding with another.
With Objective-C. Say I have two methods, setupGame and playGame. The user clicks a button that will call setupGame. Then they click another button to play the game, which will call playGame. I have setup an instance of Game in the setupGame method, however I'm not sure how you would pass it to playGame. Here's the code: [CODE] #import "AppController.h" #import "Game.h" @implementation AppController //awakeFromNib - Called when the program is run to setup the .xibs - (void) awakeFromNib { [gameStatus setStringValue: @"No Game Being Played"]; } //When setup button is pressed - (IBAction)setupGame:(id)sender { [gameStatus setStringValue: @"Setting up game..."]; Game *myGame = [[Game alloc]init]; [myGame setupPlayers]; [myGame setupDeck]; [myGame dealCards]; [myGame selectBriscolaSuit]; [myGame selectPlayerStart]; } - (IBAction)playGame:(id)sender { [myGame playGame]; [myGame gameWinner]; [myGame newGame]; } @end [/CODE] The error I'm getting is in the playGame method 'Unknown receiver myGame'. Would doing this work? [CODE] #import "AppController.h" #import "Game.h" @implementation AppController [B]Game *myGame;[/B] //awakeFromNib - Called when the program is run to setup the .xibs - (void) awakeFromNib { [gameStatus setStringValue: @"No Game Being Played"]; } //When setup button is pressed - (IBAction)setupGame:(id)sender { [gameStatus setStringValue: @"Setting up game..."]; [B]myGame = [[Game alloc]init];[/B] [myGame setupPlayers]; [myGame setupDeck]; [myGame dealCards]; [myGame selectBriscolaSuit]; [myGame selectPlayerStart]; } - (IBAction)playGame:(id)sender { [myGame playGame]; [myGame gameWinner]; [myGame newGame]; } @end [/CODE]
Does anyone have by any chance a sample premake4 script file for creating dynamic libraries?
[QUOTE=darkrei9n;35832215]Goal is to see if the sound is resetting to 0 seconds properly. Try calling stop before playing it again to ensure its reset to the start of the sound.[/QUOTE] "Again"? But it doesn't play the first time! :v: I'll try do Stop(), maybe even SetPlayingOffset(), before I attempt to play the sound at all. I'll also try printing the sound's status after I tell it to play. This is very frustrating. [editline]5th May 2012[/editline] [QUOTE=Protocol7;35832743]Just saying, in your class implementation files (.cpp) you are terminating the function curly brackets with semicolons. Not sure it effects a whole lot but that should be reserved for class declaration, not function implementation[/QUOTE] That's no issue, I can change that easily enough. Just decided to cover my bases and do that for everything, since the ";" after "}" for class implementation gave me over a day of stress. :v:
[QUOTE=Gmod4ever;35832023][b]SoundManager_Object (.h, then .cpp):[/b] [code]#ifndef _MANOBJ_SOUND_H_ #define _MANOBJ_SOUND_H_ #include <SFML/Audio.hpp> using namespace std; class ManagerObject_Sound { public: string filepath; sf::SoundBuffer buffer; ManagerObject_Sound(string); sf::SoundBuffer GetBuffer(); bool IsCorrectBuffer(string); }; #endif[/code][/QUOTE] You're returning a sf::SoundBuffer by value in GetBuffer(), which gets destroyed after the call, which is why the sound isn't playing. Return a reference instead: sf::SoundBuffer &GetBuffer().
Hey I've got a quick question. A couple more of these may pop up because I am doing my final project for my Introduction to Programming in C++ class today. Here it is: Is there anything particularly "wrong" or "bad" about using a do-while loop structure for an end-of-file loop? I'm supposed to read float values from a text file but for each value I've got to run it through several functions and I wrote much of the code before I realized that our instructor ALWAYS had us use while loops whenever we were reading values from a file. I just want to be sure that there's nothing bad about doing it the way I'm doing it, because my instructor is kind of a stingy fellow, and I don't want to get a giant penalty for doing it this way. [editline]blahblahblah[/editline] Nevermind, I answered myself after it fucked up my whole program by reading in the last value twice
[QUOTE=Z_guy;35836864]You're returning a sf::SoundBuffer by value in GetBuffer(), which gets destroyed after the call, which is why the sound isn't playing. Return a reference instead: sf::SoundBuffer &GetBuffer().[/QUOTE] Hmm. I took your advice, and changed it accordingly: [b]SoundManager_Object (.h, then .cpp):[/b] [code]class ManagerObject_Sound { public: string filepath; sf::SoundBuffer buffer; ManagerObject_Sound(string); sf::SoundBuffer &GetBuffer(); bool IsCorrectBuffer(string); };[/code] [code]sf::SoundBuffer &ManagerObject_Sound::GetBuffer() { return buffer; };[/code] It compiles fine, and doesn't error or anything in runtime, but it still plays no sound. It behaves exactly like before - buffer creates first time, is simply pointed to the other times, but no sound plays ever. :( [editline]5th May 2012[/editline] For shits and giggles, I attempted to play the sound as soon as the buffer is made and loaded, and print what filepath it loads, and the status of the sound. This is the code I use for it: [b]manobj_sound.cpp:[/b] [code]#include <SFML/Audio.hpp> #include "manobj_sound.h" #include <iostream> ManagerObject_Sound::ManagerObject_Sound(string fpath) { filepath = fpath; buffer.LoadFromFile(fpath); sf::Sound SoundInstance(buffer); SoundInstance.SetBuffer(buffer); SoundInstance.SetVolume(100); SoundInstance.SetPitch(1); SoundInstance.SetLoop(true); SoundInstance.SetPosition(0,0,0); SoundInstance.SetRelativeToListener(false); SoundInstance.SetMinDistance(1); SoundInstance.SetAttenuation(1); SoundInstance.Play(); cout << "MO_Sound created and sound played from buffer loaded from path (" << fpath << "). Status: " << SoundInstance.GetStatus(); };[/code] It prints, and it prints the correct filepath and it prints the status being 2, which is is "Playing." So I don't know what's going on. [editline]5th May 2012[/editline] I've looked around it, and I've seen a few people say (to others with similar problems) "you have to keep the sf::Sound alive." I can't find anyone anywhere explaining what the hell that means, though, or how to do it. :|
[QUOTE=Gmod4ever;35837572]I've looked around it, and I've seen a few people say (to others with similar problems) "you have to keep the sf::Sound alive." I can't find anyone anywhere explaining what the hell that means, though, or how to do it. :|[/QUOTE] Oh...right.. the sf::Sound falls out of scope at the end of the function. You have to store it somewhere..
[QUOTE=Z_guy;35840134]Oh...right.. the sf::Sound falls out of scope at the end of the function. You have to store it somewhere..[/QUOTE] Do you mean I should like, make a vector or something to store it until its duration is over, where I kill it? How exactly should I handle this? :v:
[QUOTE=Gmod4ever;35841217]Do you mean I should like, make a vector or something to store it until its duration is over, where I kill it? How exactly should I handle this? :v:[/QUOTE] As a test to see if it solves the problem, you could try to store it in the manager. But otherwise you'd want to handle it in a similar way as your sf::Sprites.
[QUOTE=Z_guy;35844111]As a test to see if it solves the problem, you could try to store it in the manager. But otherwise you'd want to handle it in a similar way as your sf::Sprites.[/QUOTE] Well, I decided to take your suggestion and do that. I modified the [b]SoundManager_Object[/b] to look as such (.h, then .cpp): [code]class ManagerObject_Sound { public: string filepath; sf::SoundBuffer buffer; sf::Sound SoundInstance; ManagerObject_Sound(string); sf::SoundBuffer &GetBuffer(); bool IsCorrectBuffer(string); };[/code] [code]ManagerObject_Sound::ManagerObject_Sound(string fpath) { filepath = fpath; buffer.LoadFromFile(fpath); sf::Sound SoundInstance(buffer); SoundInstance.SetBuffer(buffer); SoundInstance.SetVolume(100); SoundInstance.SetPitch(1); SoundInstance.SetLoop(true); SoundInstance.SetPosition(0,0,0); SoundInstance.SetRelativeToListener(false); SoundInstance.SetMinDistance(1); SoundInstance.SetAttenuation(1); SoundInstance.Play(); cout << "MO_Sound created and sound played from buffer loaded from path (" << fpath << "). Status: " << SoundInstance.GetStatus() << ", Duration: " << buffer.GetDuration() << std::endl; };[/code] And it compiles fine, but as soon as it starts to play a sound, well... [t]http://dl.dropbox.com/u/8416055/WellThen.png[/t] And this is after an error window comes up saying something about an access violation reading location :v: And about the way I manage sprites... Yeah... I haven't gotten to making a sprite manager yet. I wanted to do sounds first, because there's less involved in testing those. :v:
[QUOTE=Gmod4ever;35844421]Well, I decided to take your suggestion and do that. I modified the [b]SoundManager_Object[/b] to look as such (.h, then .cpp): [code]class ManagerObject_Sound { public: string filepath; sf::SoundBuffer buffer; sf::Sound SoundInstance; ManagerObject_Sound(string); sf::SoundBuffer &GetBuffer(); bool IsCorrectBuffer(string); };[/code] [code]ManagerObject_Sound::ManagerObject_Sound(string fpath) { filepath = fpath; buffer.LoadFromFile(fpath); sf::Sound SoundInstance(buffer); SoundInstance.SetBuffer(buffer); SoundInstance.SetVolume(100); SoundInstance.SetPitch(1); SoundInstance.SetLoop(true); SoundInstance.SetPosition(0,0,0); SoundInstance.SetRelativeToListener(false); SoundInstance.SetMinDistance(1); SoundInstance.SetAttenuation(1); SoundInstance.Play(); cout << "MO_Sound created and sound played from buffer loaded from path (" << fpath << "). Status: " << SoundInstance.GetStatus() << ", Duration: " << buffer.GetDuration() << std::endl; };[/code] And it compiles fine, but as soon as it starts to play a sound, well... [t]http://dl.dropbox.com/u/8416055/WellThen.png[/t] And this is after an error window comes up saying something about an access violation reading location :v: And about the way I manage sprites... Yeah... I haven't gotten to making a sprite manager yet. I wanted to do sounds first, because there's less involved in testing those. :v:[/QUOTE] I'm not sure why it crashes, but you forgot to remove the "sf::Sound SoundInstance(buffer);" line from the constructor, so you're still not using the one that is stored in the class. Remove that line and it should work.
[QUOTE=Z_guy;35844474]I'm not sure why it crashes, but you forgot to remove the "sf::Sound SoundInstance(buffer);" line from the constructor, so you're still not using the one that is stored in the class. Remove that line and it should work.[/QUOTE] Fair enough. Commented out that line from the constructor. It doesn't crash any more, but now it's right back to where we had begun. Also fun fact, when it was crashing, if I pressed "continue" fast enough through the horde of error popups that came up, I could actually hear my sound play - it only played a fraction of a second at a time, in the time it took for one message box to close and another to open, but it [b]was[/b] playing. :v:
[QUOTE=Gmod4ever;35844533]Fair enough. Commented out that line from the constructor. It doesn't crash any more, but now it's right back to where we had begun. Also fun fact, when it was crashing, if I pressed "continue" fast enough through the horde of error popups that came up, I could actually hear my sound play - it only played a fraction of a second at a time, in the time it took for one message box to close and another to open, but it [b]was[/b] playing. :v:[/QUOTE] Can you show the code where you're creating your ManagerObject_Sound instance?
Yeah, no problem. It's being made in the [b]Manager_Sound[/b], shown below. As always, .h then .cpp. [code]class Manager_Sound { public: int CachedBufferIndex; std::vector<ManagerObject_Sound> ObjectVector; Manager_Sound(); void ResetCachedBuffer(); void CacheBufferIndex(string); bool HasBuffer(string); bool PlaySoundInternal(string,bool,float,float,float,float,float,bool,float,float); // Holy overload (will have lots of overloads when everything is wroking) bool PlaySound(string); };[/code] [code]bool Manager_Sound::PlaySoundInternal(string fpath,bool loop,float pitch, float volume,float x, float y, float z,bool disable_spatialization, float mindist, float atten) { bool NewBuffer = false; CacheBufferIndex(fpath); if (!HasBuffer(fpath)) // We don't have a buffer for this? We better make one! { // First create the object and push it ManagerObject_Sound bufferObj(fpath); ObjectVector.push_back(bufferObj); // Then use it to play a sound sf::Sound SoundInstance(bufferObj.GetBuffer()); SoundInstance.SetBuffer(bufferObj.GetBuffer()); SoundInstance.SetVolume(volume); SoundInstance.SetPitch(pitch); SoundInstance.SetLoop(loop); SoundInstance.SetPosition(x,y,z); SoundInstance.SetRelativeToListener(disable_spatialization); SoundInstance.SetMinDistance(mindist); SoundInstance.SetAttenuation(atten); SoundInstance.Play(); NewBuffer = true; } else { // We already have a buffer for this; just use that buffer ManagerObject_Sound bufferObj = ObjectVector[CachedBufferIndex]; // Then use it to play a sound sf::Sound SoundInstance(bufferObj.GetBuffer()); SoundInstance.SetVolume(volume); SoundInstance.SetPitch(pitch); SoundInstance.SetLoop(loop); SoundInstance.SetPosition(x,y,z); SoundInstance.SetRelativeToListener(disable_spatialization); SoundInstance.SetMinDistance(mindist); SoundInstance.SetAttenuation(atten); SoundInstance.Play(); NewBuffer = false; } // At the end of the day, return whether or not we had to make a new buffer. return NewBuffer; } bool Manager_Sound::PlaySound(string fpath) { return PlaySoundInternal(fpath,false,1.f,100.f,0.f,0.f,0.f,true,1,1); }[/code]
[QUOTE=Gmod4ever;35844746]Yeah, no problem. It's being made in the [B]Manager_Sound[/B], shown below. As always, .h then .cpp. [code]-snip-[/code][/QUOTE] Okay, I think I've figured it out. Look at these two lines: [code]ManagerObject_Sound bufferObj(fpath); ObjectVector.push_back(bufferObj);[/code] In the first line you are creating an instance of ManagerObject_Sound and the constructor is called. In the second line you are calling push_back(), which is creating a copy of bufferObj (calling the copy constructor, which doesn't play the sound). Then after the function ends, the original bufferObj is destroyed, thus destroying the sf::Sound that is playing. What you should do is moving the code in the constructor to a separate method (let's say Init()) and call that like this: [code]ObjectVector.push_back(ManagerObject_Sound()); ManagerObject_Sound &bufferObj = ObjectVector.back(); bufferObj.Init();[/code]
Okay, I understood most of that. I'm not quite sure what you mean by "the code in the constructor," though. Here's what I did, though. Firstly, I removed the debug code from [b]ManagerObject_Sound[/b], so it looks like this now (.h and .cpp): [code]class ManagerObject_Sound { public: string filepath; sf::SoundBuffer buffer; ManagerObject_Sound(string); sf::SoundBuffer &GetBuffer(); bool IsCorrectBuffer(string); };[/code] [code]ManagerObject_Sound::ManagerObject_Sound(string fpath) { filepath = fpath; buffer.LoadFromFile(fpath); };[/code] So, now it's back to what it was originally and what I want to look like (roughly; I'll change it as needed to get it working :v:). Then, in my [b]Manager_Sound[/b], I changed it from this: [code] // First create the object and push it ManagerObject_Sound bufferObj(fpath); ObjectVector.push_back(bufferObj);[/code] To this: [code]// First create the object and push it ObjectVector.push_back(ManagerObject_Sound(fpath)); ManagerObject_Sound &bufferObj = ObjectVector.back();[/code] Which looks more or less like what you suggested. I'm just not exactly sure what all code you want me to scoop out of the constructor and put into its own method. Just for clarification, [b]Sound_Manager.cpp[/b]'s relevant code is now as thus: [code]bool Manager_Sound::PlaySoundInternal(string fpath,bool loop,float pitch, float volume,float x, float y, float z,bool disable_spatialization, float mindist, float atten) { bool NewBuffer = false; CacheBufferIndex(fpath); if (!HasBuffer(fpath)) // We don't have a buffer for this? We better make one! { // First create the object and push it //ManagerObject_Sound bufferObj(fpath); //ObjectVector.push_back(bufferObj); ObjectVector.push_back(ManagerObject_Sound(fpath)); ManagerObject_Sound &bufferObj = ObjectVector.back(); // Then use it to play a sound sf::Sound SoundInstance(bufferObj.GetBuffer()); SoundInstance.SetBuffer(bufferObj.GetBuffer()); SoundInstance.SetVolume(volume); SoundInstance.SetPitch(pitch); SoundInstance.SetLoop(loop); SoundInstance.SetPosition(x,y,z); SoundInstance.SetRelativeToListener(disable_spatialization); SoundInstance.SetMinDistance(mindist); SoundInstance.SetAttenuation(atten); SoundInstance.Play(); NewBuffer = true; } else { // We already have a buffer for this; just use that buffer ManagerObject_Sound bufferObj = ObjectVector[CachedBufferIndex]; // Then use it to play a sound sf::Sound SoundInstance(bufferObj.GetBuffer()); SoundInstance.SetVolume(volume); SoundInstance.SetPitch(pitch); SoundInstance.SetLoop(loop); SoundInstance.SetPosition(x,y,z); SoundInstance.SetRelativeToListener(disable_spatialization); SoundInstance.SetMinDistance(mindist); SoundInstance.SetAttenuation(atten); SoundInstance.Play(); NewBuffer = false; } // At the end of the day, return whether or not we had to make a new buffer. return NewBuffer; }[/code] However, there [b]still[/b] is no sound. Thanks a lot for all the help, by the way. You guys have no idea how much this means to me. Now I just wish we can get it working soon. [editline]fish[/editline] [b][highlight]HOT[/highlight][/b] I also made it so the "SoundInstance" is created and stored in the class declaration (to "keep it alive"), and now the sound plays when the buffer is made. Yay. Also set up the "buffer already exists" to work similarly, but unfortunately, it's not yet working. Progress, though! [editline]6th May 2012[/editline] Now that I got that working, and I am onto getting the cached buffer method to work, here's what I got. [b]Manager_Sound (.h then .cpp)[/b] (There's new, relevant code now! Honest!) [code]class Manager_Sound { public: int CachedBufferIndex; std::vector<ManagerObject_Sound> ObjectVector; sf::Sound SoundInstance; Manager_Sound(); void ResetCachedBuffer(); void CacheBufferIndex(string); bool HasBuffer(string); bool PlaySoundInternal(string,bool,float,float,float,float,float,bool,float,float); // Holy overload bool PlaySound(string); };[/code] [code]void Manager_Sound::CacheBufferIndex(string fpath) { int i; for (i=0; ObjectVector.size(); i++) { if (ObjectVector[i].IsCorrectBuffer(fpath)) { CachedBufferIndex = i; break; } } } bool Manager_Sound::HasBuffer(string fpath) { return !(CachedBufferIndex==MANAGER_NULL_INDEX); } bool Manager_Sound::PlaySoundInternal(string fpath,bool loop,float pitch, float volume,float x, float y, float z,bool disable_spatialization, float mindist, float atten) { bool NewBuffer = false; CacheBufferIndex(fpath); if (!HasBuffer(fpath)) // We don't have a buffer for this? We better make one! { // You know what's here... This part is working. I won't post it AGAIN and clutter this page even more than I already have. } else { // We already have a buffer for this; just use that buffer ManagerObject_Sound bufferObj = ObjectVector[CachedBufferIndex]; // Then use it to play a sound //sf::Sound SoundInstance(bufferObj.GetBuffer()); SoundInstance.SetBuffer(bufferObj.GetBuffer()); SoundInstance.SetVolume(volume); SoundInstance.SetPitch(pitch); SoundInstance.SetLoop(loop); SoundInstance.SetPosition(x,y,z); SoundInstance.SetRelativeToListener(disable_spatialization); SoundInstance.SetMinDistance(mindist); SoundInstance.SetAttenuation(atten); SoundInstance.Play(); NewBuffer = false; } // At the end of the day, return whether or not we had to make a new buffer. return NewBuffer; }[/code] [b]ManagerObject_Sound.cpp[/b] [code]bool ManagerObject_Sound::IsCorrectBuffer(string fpath) { return (filepath==fpath); };[/code] As far as I know, the ObjectVector.back(); method and this method should be returning the same thing.
[QUOTE=Gmod4ever;35844944]Now that I got that working, and I am onto getting the cached buffer method to work, here's what I got. -snip- As far as I know, the ObjectVector.back(); method and this method should be returning the same thing.[/QUOTE] This line: [code]ManagerObject_Sound bufferObj = ObjectVector[CachedBufferIndex];[/code] You are creating a copy of the ManagerObject_Sound (which gets destroyed when the function ends). Create a reference instead: [code]ManagerObject_Sound &bufferObj = ObjectVector[CachedBufferIndex];[/code]
Oh wow I feel stupid. Could have sworn I made sure I did that... [img]http://dl.dropbox.com/u/8416055/FacepunchEmots/Saddowns.gif[/img] Thanks again, guys. It works beautifully. Now I'm off to continue my endeavors, since this challenge has finally been overcome. The lesson of the day is pointers are my friend, and I should use them.
[QUOTE=dvondrake;35844507]Not sure if you've already figured this out yet, but the general idea is to move the player forwards, test if the bounding boxes are intersecting, and if so move him back. That's how I do it at least. So long as it's all done before the frame is rendered, so you don't visually see the player moving inside the wall and bouncing back.[/QUOTE] Do you mean to move the player back to the point where he was before collision or just move it backwards one time?
I'm trying (and failing) to create a 2D array of Java.awt rectangles, no clue what's going on... [lua]/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package lab4_2; import javax.swing.*; import java.awt.*; /** * * @author Kyle */ public class Lab4_2 { /** * @param args the command line arguments */ public static void main(String[] args) { GameBoard game = new GameBoard(); final int XSIZE = 195, YSIZE = 215; //frame size JFrame win = new JFrame("Tic Tac Toe"); win.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); win.setBounds(0, 0, XSIZE, YSIZE); Rectangle panels[][] = new Rectangle[2][2]; //Array int count1 = 0, count2, colourCount = 0; //colourCount is to determine which rectangle will be red or black while(count1 <= 2) { count2 = 0; while(count2 <= 2) { //Colour choice if(colourCount % 2 == 0) { panels[count1][count2].setColor(Color.BLACK); } else { panels[count1][count2].setColor(Color.RED); } //Create rectangle panels[count1][count2].fillRect(count1*(XSIZE/3), count2*(YSIZE/3), XSIZE/3, YSIZE/3); colourCount++; count2++; } } //Add to window count1 = 0; while (count1 <=2) { count2 = 0; while (count2 <= 2) { win.getContentPane().add(panels[count1][count2]); count2++; } count1++; } win.pack(); win.setVisible(true); } } [/lua] Any clue what's wrong?
In Java for arrays I believe its typename[][] variablename rather than typename variablename[][] when initializing an array.
[QUOTE=darkrei9n;35845927]In Java for arrays I believe its typename[][] variablename rather than typename variablename[][] when initializing an array.[/QUOTE] I don't think so, I've used arrays in the past and have always done it like this, only difference is that they were String arrays rather than Rectangle Arrays
Sorry, you need to Log In to post a reply to this thread.