Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Shuffling Sprite Locations with an Array (troubles and tears)

Author
Message
Dum_Bass
10
Years of Service
User Offline
Joined: 20th Mar 2013
Location: Hiding in your closet.
Posted: 22nd Apr 2013 09:16
Using DarkGDK and VC++ 2008.

What follows is a prototype in which I intended to shuffle the elements of an array containing X and Y axis points and use this to generate random locations for sprites. Firstly, I get a compile error reporting that neither of the sprites initialized in the calling function recognized my arrays (example: brickRow undeclared identifier, brickColumn undeclared identifier). This tells me that I suck at life. I don't know why they aren't being identified when I initialized the arrays in the called function (void Levels::BrickLocations (void)). I wish to use this function within multiple class functions so it would be really super neat if it would work.

But wait! There's more!

SO... to test my first function out, and pat myself on the back when everything worked just as I had planned, I just moved all of the lines into the calling class function (void Levels::LevelOne (void)). It threw no compile errors but sure didn't work as expected. My sprites drew to locations outside of the integers I used when constructing my arrays. Don't know why. Probably black magic or something (I hear that is a common problem for programmers).

[void Levels::BrickLocations( )
{
int tempRow;
int randRow;
int lastRow;
int tempCol;
int randCol;
int lastCol;

// Stores the X axis coordinates for the bricks in all levels
int brickRow[6] = {150, 250, 350, 450, 550, 650};

// Shuffles the elements of brickRow each time the function is called
for (lastRow = 5; lastRow > 0; --lastRow)
{
randRow = rand( ) % lastRow;
tempRow = brickRow [lastRow];
brickRow [randRow] = brickRow [lastRow - 1];
brickRow [lastRow - 1] = tempRow;
}

// Stores the Y axis coordinates for the bricks in all levels
int brickColumn[7] = {125, 175, 225, 275, 325, 375, 425};

// Shuffle the elements of brickColumn each time the function is called
for (lastCol = 6; lastCol > 0; --lastCol)
{
randCol = rand( ) % lastCol;
tempCol = brickColumn [lastCol];
brickColumn [randCol] = brickColumn [lastCol - 1];
brickColumn [lastCol - 1] = tempCol;
}
return;
}

void Levels::LevelOne( )
{
brickLocations( );
// Sets up the bricks
dbSprite (REDBRICK, brickRow[0], brickColumn[0], 30);
//dbOffsetSprite (REDBRICK, 25, 50);
dbRotateSprite (REDBRICK, 0);
dbSetSpritePriority (REDBRICK, 9);
dbSprite (GLASS, brickRow[0], brickColumn[3], 31);
//dbOffsetSprite (GLASS, 25, 50);
dbRotateSprite (GLASS, 0);
dbSetSpritePriority (GLASS, 9);
return;
}]

Any help would be much appreciated. Than again, I could always look to a promising career in the fastfood industry.
Dum_Bass
10
Years of Service
User Offline
Joined: 20th Mar 2013
Location: Hiding in your closet.
Posted: 23rd Apr 2013 15:18
Alright, so I have figured out how to properly point to the elements of my arrays and use those pointers as parameters for the "dbSprite()" command.



I may even have learned how to finally post my code properly on this forum. That I will not learn until the message has passed through the moderators and is actually posted.

Still haven't gotten a proper grasp on how to call a function between classes and more importantly, my little array shuffling algorithm does not appear to be up to snuff. Woe is my life
Dum_Bass
10
Years of Service
User Offline
Joined: 20th Mar 2013
Location: Hiding in your closet.
Posted: 24th Apr 2013 15:44 Edited at: 4th May 2013 10:37
This is starting to read more like diary entries than a thread...

Dear Diary,
Today I found out about a nifty little function: random_shuffle. I used it and to my delight I found that it accomplished exactly what I was trying to do without all of that messy math stuff. That is just swell but it does feel a little like cheating since my ultimate goal is to better understand programming in c++ vs. piggybacking off the work of others; but it works for now. It works too well. Not only are my two little sprites randomly drawing to locations within my defined ranges, they just won't stop redrawing. Its sprites run amok! Hmmm...?



Well, at the least I have figured out how to structure my code to call on class functions in a way that I already understand, so that counts for something, this bit is just a little frustrating. I wonder if hurling a javaline at my computer monitor will help?

P.S. If there are any helpful souls out there, "srand (time (0))" is located at the first line of my main loop but outside of this class and all variables used here are publically declared in my class header. I only initialize the variables at the time of use. It feels more organized to do it this way but I fear that I may be forming a bad habit.

Haikus are easy; But sometimes they don’t make sense; Pteredactyl pie
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 27th Apr 2013 12:42
Gahhh! Don't use parallel arrays, it's bad practice.




If you want to call functions from other classes, you either create an object of that class (instantiate the class) or if the function is static you can call it directly from the class itself.

MyClass.level(1) //static way

or

MyClass thing;
thing.level(1)



Also, this board is more for graphical 2D stuff and not so much for coding. You'll probably find quicker help by posting in one of the other appropriate boards.

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Dum_Bass
10
Years of Service
User Offline
Joined: 20th Mar 2013
Location: Hiding in your closet.
Posted: 30th Apr 2013 13:20 Edited at: 4th May 2013 10:41
Thanks for the input. You are the second person to recommend posting this thread elsewhere but for now I will consider it closed. I have started doing some array and class structuring exercises out of one of Bjarne Stroustrup's books to get a better understanding of how these principles work. I will implement your suggestion and avoid parallel arrays in my code (this time I thought it was necessary to conform to the arguments accepted by dbSprite()). It turns out I was wrong and found a cleaner way by calling on a single array and doing some of that "messy math stuff".

Haikus are easy; But sometimes they don’t make sense; Pteredactyl pie
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 6th May 2013 16:55
One thing to suggest, is that you edit your first post, rather than adding posts... see what I tend to do, and probably other people as well - is to look for threads with no replies, because that's signifies a problem that hasn't been solved, but a thread with 4 or 5 replies has been solved usually - so people seeing your thread with no replies is a lot more fruitful than seeing your thread with even just a handful of replies.

Anyway, good to see you figured it out, and are learning new stuff as well, just wanted to chip in my 2p's worth.

I got a fever, and the only prescription, is more memes.

Login to post a reply

Server time is: 2024-02-23 22:52:35
Your offset time is: 2024-02-23 22:52:35