Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 39 40 [41] 42 43 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100892 times)

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Help Thread (For Dummies)
« Reply #600 on: April 15, 2012, 09:15:18 pm »

MIDI is completely unrelated to WAV, so it's rather odd that the stack trace includes MIDI methods. And that, combined with the specific fact that it's a NullPointerException makes it somewhat googlable.

http://www.dreamincode.net/forums/topic/246459-nullpointerexception-on-audioinputstram-and-url/
(Must be an obscure problem, as that was the only related result)

Logged
Eh?
Eh!

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #601 on: April 15, 2012, 09:52:04 pm »

Well, getResource returns null if the resource name passed to it doesn't exist, so there is probably something wrong with fileName.

The fileName is most certainly Pop.wav Good idea though

When I do it outside of the JAR, I can do it this way, which works just fine.
Code: [Select]
File soundFile = new File("C:/Users/Me/Desktop/Java" + fileName);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();

And qwertyuiopas, that seems not to be quite the same thing, as they are trying to get something off the internet and I'm trying to get something from a JAR. I tried the thing they said worked for them, but it didn't work for me. However, I don't really know, and I could be looking at it a completely wrong way.

I do have a workaround of storing the sounds outside the JAR, but I'd like a better way. Thank you guys for your help (everybody on this thread), I wouldn't know half as much Java without you.
« Last Edit: April 15, 2012, 09:57:02 pm by Kofthefens »
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

nobodynothing

  • Escaped Lunatic
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #602 on: April 16, 2012, 12:05:06 pm »

If anyone has any knowlege of SFML2 (More specifically the new Release Candidate), I've got some questions about the implementation of a tile engine using a VertexArray for performance. Currently my project draws each tile to a RenderTexture, then draws that entire texture to the window at once, which is horribly slow.

Laurent posted an example of a tilemap using a vertex array, but I've been having trouble understanding exactly what's going on and how I can implement something like that myself.
Logged

AlStar

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #603 on: April 19, 2012, 01:08:30 pm »

I don't know how much anyone will be able to help me, since I'm at work at the moment and can't post the actual code, but I figure I'll post what I can now, and maybe someone can think up why my program is misbehaving like it is. If no one can, I'll try again when I get home.

Ok, programming in Java, I've got a class called 'Item'. Within 'Item' is a String called 'type'. I've got another class called 'Creature', which implements a number of Items - 9 of them, in fact, named 'slot1' through 'slot9'.
Initially these Items are just created (Item slot1 = new Item();), but later I actually turn them into stuff, and that's where things are getting strange.

For a given creature (a Humanoid, in this case), my code tells it to do this:
slot1.type = "head";
slot2.type = "torso";
slot3.type = "legs";
slot4.type = "feet";
slot5.type = "carry";
slot6.type = "ring";
slot7.type = "ring";
slot8.type = "weapon";
slot9.type = "shield";

The problem is that all nine slots end up as 'type' "shield".

Throwing in System.out.Print(slot1.type); gives the following:
Right after slot1 is 'type'd, it returns "head".
Right after slot2 is 'type'd, it returns "torso".

Somehow, it seems that all my Items are pointing to the same marker; but I don't see any reason why they should do such a thing.

Any ideas what's going on and/or how to fix it? Would throwing the Items into an array make any difference?
« Last Edit: April 19, 2012, 01:10:59 pm by AlStar »
Logged

Lord Dullard

  • Bay Watcher
  • Indubitably.
    • View Profile
    • Cult: Awakening of the Old Ones
Re: Programming Help Thread (For Dummies)
« Reply #604 on: April 19, 2012, 01:17:08 pm »

When you get home, post the relevant bits of code. I'd like to take a look at them and see if I can come up with a good guess.
Logged

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #605 on: April 19, 2012, 01:23:17 pm »

Daaaamn, I haven't been here in ages.
And I haven't programmed in a while either.
Must fix. :P
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

AlStar

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #606 on: April 19, 2012, 05:02:17 pm »

Ok, so the relevant chunks of code:
Spoiler (click to show/hide)

It seems to be the first bit of NewCreature or even earlier when I create a Creature that things are getting mixed up, because the creation code will make items... well, to be more accurate, it will make an item - the same one shows up in all nine slots.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #607 on: April 19, 2012, 05:09:03 pm »

Well, evidently you don't know what the static keyword means for class members because it means that that member belongs to the class and not any particular instance, so you only have one item type. Incidentally, the compiler should probably warn you if you try to make multiple copies of a all-static class.
Logged

AlStar

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #608 on: April 19, 2012, 06:34:32 pm »

You know, it's funny - I've just pretty much always used static in pretty much everything. I'm surprised it hasn't bitten me in the ass before this.

un-static-ing the relevant portions of the code fixed it - thanks Ruler.

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #609 on: April 20, 2012, 02:38:50 am »

You know, it's funny - I've just pretty much always used static in pretty much everything. I'm surprised it hasn't bitten me in the ass before this.

As a Java programmer this really hurt me on the inside. Static is used to declare shared variables and global classes within classes (or run static blocks of code). Only use it when you actually need it.

Also, you have several other weird things going on in your code. '==' cannot be used to compare strings in Java, you need to use String.equals(). Also, it's really strange to do "Knight".equals(Y). I suppose it works, but it's not idiomatic. Whenever programmers have a large list of variables that differ only in indexing, we use arrays, i.e. slot1, slot2, ... should instead be Item[] slots = new Item[8] (note that you still have to individually initialize each slot, since initializing arrays of objects in Java sets all the values in the array to null).

You should use more descriptive variable names. A function with arguments A, B, C, b, c, d, e, f isn't going to tell you much (even if you've written comments, it's just so much easier to program if your variable names are in plain english), you're bound to forget what everything does, and calling it item() is kinda undescriptive. Variables in Java are usually multi-word things written in camelCase, i.e. lowercase first letter and uppercase letters whenever you reach a new word.

Consider reading up a bit more on programming in Java before proceeding with your project, especially since you don't seem to yet have a grip on OOP, which is vital to programming in Java.
« Last Edit: April 20, 2012, 02:41:07 am by Normandy »
Logged

malloc

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #610 on: April 20, 2012, 03:29:57 am »

Staticness or globalness should really only be used when there is some global service that have to be managed. Even then you can always almost always avoid it.

You should only use it when you are using some very lowlevel code. A class to manage a threadpool is a potential candidate for global class, or a resource manager. But even then the one can interface to the global by using proper OOP.

OOP offers a lot of help for the compiler, which can help optimizing code output. Especially newer compilers will produce highly optimized code by following proper OOP.
Logged

AlStar

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #611 on: April 20, 2012, 06:48:52 am »

Also, you have several other weird things going on in your code. '==' cannot be used to compare strings in Java, you need to use String.equals().

Yes, my compiler keeps bitching at me about that too, but either it's smart enough to swap it behind the scenes, or it works anyway, even if it's not proper form, because I've successfully used it in other programs before. I find == to be much easier for me to be able to tell what something does at a glance.

Quote
Also, it's really strange to do "Knight".equals(Y). I suppose it works, but it's not idiomatic.

And then, in the one part of the code where I decided to try it out, you've got problems with my variable names? Really?


Quote
Whenever programmers have a large list of variables that differ only in indexing, we use arrays, i.e. slot1, slot2, ... should instead be Item[] slots = new Item[8] (note that you still have to individually initialize each slot, since initializing arrays of objects in Java sets all the values in the array to null).
I've used arrays before; I actually changed Item to be an array while I was trying to figure out why it wasn't working here. That said, it honestly didn't seem to change anything - I literally just did a "find-replace" for "Item#" with "Item[#]" (making sure to add in a Item[0], of course). Is there any real benefit for having it in an array?

Quote
You should use more descriptive variable names. A function with arguments A, B, C, b, c, d, e, f isn't going to tell you much (even if you've written comments, it's just so much easier to program if your variable names are in plain english), you're bound to forget what everything does, and calling it item() is kinda undescriptive. Variables in Java are usually multi-word things written in camelCase, i.e. lowercase first letter and uppercase letters whenever you reach a new word.
I use descriptive variables where they make a difference. I suppose I could have named that function CreateItem(String TypeofItem,String CreatureWears,String CreatureWields,int EpicChance,int GreaterMagicChance,int MagicChance,int LesserMagicChance,int CommonChance)... but you know what? Those variables are only used during item creation, and are immediately used to generate random numbers, nothing more; I find it a lot easier to just note with a quick //.

Quote
Consider reading up a bit more on programming in Java before proceeding with your project, especially since you don't seem to yet have a grip on OOP, which is vital to programming in Java.
How dare you question my vast experience - I took a single college-level class over a decade ago! I'm totally a pro!  :P

I'll admit that I'm rusty, which is probably what caused this problem here (I looked through some of my old programs afterwards, and saw where I ran into the same thing- I must've forgotten in the several year hiatus from programming I took.) I honestly have no idea where I got it into my head that everything should be static - I'll have to try and beat it out of my coding.

Anyway, sorry if I come across as sorta britsly Normandy, but I'll note that this topic is called (For Dummies), and (believe it or not) I've actually managed to code some fairly complex stuff (under the hood, anyway - making a working graphical UI is apparently right at the edge of my abilities, since it's giving me a bitch of a time on something else I'm working on.) And don't think I don't appreciate the pointers... just not so much the condescending attitude that came with it.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #612 on: April 20, 2012, 01:01:08 pm »

It's Java, you're supposed to name your variables by using this website (http://projects.haykranen.nl/java/) and pressing the button at least 5 times.

Spoiler: Obligatory (click to show/hide)
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #613 on: April 20, 2012, 01:02:32 pm »

It's Java, you're supposed to name your variables by using this website (http://projects.haykranen.nl/java/) and pressing the button at least 5 times.

Haha, so true.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #614 on: April 20, 2012, 04:59:07 pm »

@AlStar:
Responding to your points in order,

This is actually a bit of dark compiler magic. Whenever you have identical string constants in a program, they are all assigned the same memory address (reference), so the == comparison works since they're all the same object. However, if you were to enter the string during runtime, then it would not work.

"Knight" is not a variable name. It's a string constant. Y is a variable name.

The benefit of making things arrays is that it's more easily extensible, i.e. if you wanted more or less slots you can just change a single number (i.e. declare a final int numSlots), and there's a lot less copy-pasting of code.

It's just a bad habit to not give actual names to your variables. It decreases code readability which in turn increases time spent programming. Especially if other people need to read your code. There are cases when you don't need to give variables descriptive names (such as index variables, when the variables follow natural english (i.e. equals(x)), or when using commonly accepted abbreviations (such as v for velocity). Otherwise, it'll just make code a pain to read and debug.

As for a condescending attitude, blame it on the internet. Imagine me reading them not in a haughty tone but in a helpful tone. I'm trying to be as straightforward as possible, so I am indeed being quite terse and unapologetic, which more often then not comes across as condescending without the benefit of body language or intonation.
Logged
Pages: 1 ... 39 40 [41] 42 43 ... 91