Spiny Dobopty

Member
Posts: 102
Joined: 2005.01
Post: #1
I think the title says it all.
This is for the same game as my last post and i am being lame again i think Annoyed
I want it to so when you click on a gem owned by you it spins the 8 ajacent jems counter clockwise and when you ctrl+click it spints them clockwise.
so far i have this:

Code:
if (x > 10 && x < [view bounds].size.width-10 &&
        y > 10 && y < [view bounds].size.height-10)    {
            x = (x-(x%32)+10);
            y = (y-(y%32)+10);
            for ( a = 0 ; a < [gems count] ; a++ )    {
                if ([(Gem *)[gems objectAtIndex:a] x] == x &&
                    [(Gem *)[gems objectAtIndex:a] y] == y &&
                    [(Gem *)[gems objectAtIndex:a] owner] == turn)    {
                        
                }
            }
    }


x and y being the coordinates of the mouseDown event the tiles are 32x32 and there is a 10 pixel boarder around the screen thus all the tens. Can someone enlighten me as to how to get them to spin clockwise in that if, the "Gems" have are methods -x -y -owner
and -index, index refers to which index they are in the main game board array.

All I know is I will need an array to hold all ajacent tiles after that my mind goes in circles and i fall asleep Sad.
Thanks
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
I would do it by having an array of arrays for the gems. Basically, you have one array that holds all the rows, then for each row you have an array for the gems in that row. So when you get the 2 indices that correspond to where you click, you can get the 8 adjacent gems and move them to the next position.

BTW, with Objective C you don't have to cast the object coming out of the array. It already knows which method it needs to call.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #3
It gets mad if i remove it (the cast) because it (the compiler) thinks im trying to use NSObject's -owner.

also i read that too late and here i am:

Code:
if (x > 10 && x < [view bounds].size.width-10 &&
        y > 10 && y < [view bounds].size.height-10)    {
            x = (x-(x%32)+10);
            y = (y-(y%32)+10);
            for ( a = 0 ; a < [gems count] ; a++ )    {
                if ([(Gem *)[gems objectAtIndex:a] x] == x &&
                    [(Gem *)[gems objectAtIndex:a] y] == y &&
                    [(Gem *)[gems objectAtIndex:a] owner] == turn)    {
                        for ( a = 0 ; a < [gems count] ; a++ )    {
                            //left move up
                            if ([(Gem *)[gems objectAtIndex:a] x] == x-32 &&
                                [(Gem *)[gems objectAtIndex:a] y] == y)    {
                                [game setY:[(Gem *)[gems objectAtIndex:a] y]+32 ofGem:a];
                            }
                            //left up move right
                            if ([(Gem *)[gems objectAtIndex:a] x] == x-32 &&
                                [(Gem *)[gems objectAtIndex:a] y] == y+32)    {
                                [game setX:[(Gem *)[gems objectAtIndex:a] x]+32 ofGem:a];
                            }
                            //up move right
                            if ([(Gem *)[gems objectAtIndex:a] x] == x &&
                                [(Gem *)[gems objectAtIndex:a] y] == y+32)    {
                                [game setX:[(Gem *)[gems objectAtIndex:a] x]+32 ofGem:a];
                            }
                            //up right move down
                            if ([(Gem *)[gems objectAtIndex:a] x] == x+32 &&
                                [(Gem *)[gems objectAtIndex:a] y] == y+32)    {
                                [game setY:[(Gem *)[gems objectAtIndex:a] y]-32 ofGem:a];
                            }
                            //right move down
                            if ([(Gem *)[gems objectAtIndex:a] x] == x+32 &&
                                [(Gem *)[gems objectAtIndex:a] y] == y)    {
                                [game setY:[(Gem *)[gems objectAtIndex:a] y]-32 ofGem:a];
                            }
                            //down right move left
                            if ([(Gem *)[gems objectAtIndex:a] x] == x+32 &&
                                [(Gem *)[gems objectAtIndex:a] y] == y-32)    {
                                [game setX:[(Gem *)[gems objectAtIndex:a] x]-32 ofGem:a];
                            }
                            //down move left
                            if ([(Gem *)[gems objectAtIndex:a] x] == x &&
                                [(Gem *)[gems objectAtIndex:a] y] == y-32)    {
                                [game setX:[(Gem *)[gems objectAtIndex:a] x]-32 ofGem:a];
                            }
                            //down left move up
                            if ([(Gem *)[gems objectAtIndex:a] x] == x-32 &&
                                [(Gem *)[gems objectAtIndex:a] y] == y-32)    {
                                [game setY:[(Gem *)[gems objectAtIndex:a] y]+32 ofGem:a];
                            }
                        }
                }
            }
    }

yea its ugly and hard to read.That seems to me like it should work but it doesnt remotly, all it does right now is make all the gems move to the left side.... very depressing. Why do code tags add 700 tabs? anyone i cant read my code in a code tag!!!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
Are you sure your set methods are correct? Why are you doing this: (x-(x%32)+10)? If you have your grid set up from 0 to whatever in increments of 32 (when processing it), you should just subtract 10 from x and y, then see if it's >= location and <= location + 32 (for both x and y). Also, that will be extremely slow. You're looping through all the gems 17 times: once to see which one, once for each of the 8 gems around, and for each of those times, you are looping through to set the new coordinate.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #5
I only see 2 loops. and there is a 10 px boarder around the board thus the 10s.
What are you saying im doing about the looping to set the new coordinate? and yea the set method works, i used it in all my previous movment stuff and those work fine.

Edit: i think i see what is happening. There arnt any elses so they are all happening so its pushing the gems around and around. OR something of that sort

Edited Edit: I added elses for all exept the first one, it works now exept on the first mouse down, and there is no bounds protection
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
I don't know why, but I thought you'd be going into a loop when setting the X and Y to find the gem. Hey, it was late! Rasp (also, I would have put the set methods in the gem object itself, but that's just me) But the fact remains that it must loop through all of the gems for each of the 8 gems you are searching for. If you used the double-array method, you'd get the x and y coordinates, you could get the 2 indices from those coordinates, then directly figure out the other indices from that. No loops required. You can do the same thing with 1 large array, but it would be a little more difficult.

Edit: the only thing I can think of for your other problem is possibly your event-handling code and the way you change the x and y coordinates to something you can check for indices.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #7
There is a seX in the gem object, but i have an array of gems that are drawn so i have to change that ones X, so it has to be in Game.

Also i got it to work bascially but the only thing is that the out of bounds protection is going to be tricky Annoyed how should i handle that.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #8
As in you go all the way to the right and it starts shifting ones you don't want? You can check to see if it's all the way in one direction, don't process the ones beyond.
Quote this message in a reply
Post Reply