Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2011, 08:48 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 4
phantomsri is on a distinguished road
Default random sprite position

hi i have a game, right now i need a advice how to code a code.

I have 4 sprites in a 2x2 arrangement. all the sprite is different.
How can i random/mix the position when i load the layer?

So far I have done this code. Is there a simpler way or this method im using is correct? I have plans to load more sprites in 5X5 arrangement.

Code:
CGPoint grid1 =ccp(60,210);
CGPoint grid2 =ccp(60,280);
CGPoint grid3 =ccp(180,210);
CGPoint grid4 =ccp(180,280);

sprite1 =[CCSprite spriteWithFile:@"sprite1"];
sprite2 =[CCSprite spriteWithFile:@"sprite2"];
sprite3 =[CCSprite spriteWithFile:@"sprite3"]; 
sprite4 =[CCSprite spriteWithFile:@"sprite4"];

int randomLocation = (arc4random() % 4) + 1;

switch(randomLocation)
{
case 1:

sprite1.position=grid1;
sprite1.position=grid2;
sprite1.position=grid3;
sprite1.position=grid4;

break;

case 2:

sprite1.position=grid2;
sprite1.position=grid3;
sprite1.position=grid1;
sprite1.position=grid4;

break;

case 3:
sprite1.position=grid4;
sprite1.position=grid1;
sprite1.position=grid2;
sprite1.position=grid3;

break;

case 4:
sprite1.position=grid4;
sprite1.position=grid3;
sprite1.position=grid2;
sprite1.position=grid1;

break;
}

[self addChild:sprite1];
[self addChild:sprite2];
[self addChild:sprite3];
[self addChild:sprite4];
phantomsri is offline   Reply With Quote
Old 08-15-2011, 09:10 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Location: Edinburgh
Posts: 209
Justinmichael is on a distinguished road
Default

As I think you are finding out, code like this will work, but becomes unmanageable.

Try reading up on Object Oriented Programming as there are many ways you can make this more manageable.

Object-oriented programming - Wikipedia, the free encyclopedia
__________________
Justinmichael is offline   Reply With Quote
Old 08-15-2011, 11:01 AM   #3 (permalink)
Registered Member
 
headkaze's Avatar
 
Join Date: Feb 2010
Posts: 359
headkaze is on a distinguished road
Default

I think a CCSprite is a Cocos2D sprite object. If so set up a multidimensional array of CCSprite objects. That way you can have rows and columns of sprites. Rather than randomly set their position, randomly select their skin ("texture"). To set their position use...

Code:
for(int y=0; y<NUM_ROWS; y++)
    for(int x=0; x<NUM_COLS; x++)
        // Set position to x * WIDTH_OF_SPRITE, y * HEIGHT_OF_SPRITE
For the random skin, have an array of skins and randomly select one and set it for each sprite.

PS Learning OOP certainly wouldn't hurt
__________________
Headsoft | Jungool
headkaze is offline   Reply With Quote
Old 08-15-2011, 09:04 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 44
new2objectivec is on a distinguished road
Default

Quote:
Originally Posted by phantomsri View Post
hi i have a game, right now i need a advice how to code a code.

I have 4 sprites in a 2x2 arrangement. all the sprite is different.
How can i random/mix the position when i load the layer?

So far I have done this code. Is there a simpler way or this method im using is correct? I have plans to load more sprites in 5X5 arrangement.
How about create 2 arrays: 1st for the grid, 2nd for the sprite.
when assigning values to 2nd array, set the position to a special value (0,0 or something special)

then use the random function to assign each of them to one of the grids, if the "position" of that sprite is that special value, you know it's not been assigned yet and can be safely updated.

hope this helps
new2objectivec is offline   Reply With Quote
Old 08-16-2011, 06:41 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 4
phantomsri is on a distinguished road
Default

Quote:
Originally Posted by new2objectivec View Post
How about create 2 arrays: 1st for the grid, 2nd for the sprite.
when assigning values to 2nd array, set the position to a special value (0,0 or something special)

then use the random function to assign each of them to one of the grids, if the "position" of that sprite is that special value, you know it's not been assigned yet and can be safely updated.

hope this helps
sorry im a little confused. Are you reffering to use NSarrays?
can help provide a little sample code.
im still learning on cocos2d ios dev.
phantomsri is offline   Reply With Quote
Old 08-24-2011, 08:19 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 44
new2objectivec is on a distinguished road
Default

2nd try, probably something like this:

Code:
#define maxAry 4

CGPoint grid[maxAry];
Sprite sprite[maxAry];    // this might not be right as it wasn't specified in your original code what data type the sprites are

-(void)randomAssign {
	BOOL assigned[maxAry];
	int jj,kk;

	//initialize
	for (int ii=0; ii<maxAry; ii++) {
		assigned[ii]=NO;
	}
	
	jj=0;
	kk=0;
	do {
		kk = arc4random() % maxAry;

		if (!assigned[kk]) {
			sprite[jj].position=grid[kk];
			assigned[kk]=YES;
			jj++;	
		}
		
	} while (jj<maxAry)
	
}

//in the main code area

[self randomAssign];

Last edited by new2objectivec; 08-24-2011 at 08:32 PM.
new2objectivec is offline   Reply With Quote
Reply

Bookmarks

Tags
random

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 413
11 members and 402 guests
AppleDev, chemistry, Emy, Gi-lo, ipodphone, mistergreen2011, pipposanta, Retouchable, skrew88, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,923
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:21 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0