Re: [iphonedev.se] behöver hjälp med 3 i rad spel

8 views
Skip to first unread message
Message has been deleted

Erik Hedegren

unread,
Dec 4, 2011, 3:11:57 PM12/4/11
to iphone...@googlegroups.com
Tja,

Ett tips när du ber om hjälp är att skriva mer specifika frågor. Du har postat 13 sidor kod här, vilket kommer ta ett tag att läsa och sätta sig in i, och de flesta har inte tid med det.


On 4 dec 2011, at 04:44, Christos wrote:

> Hej Allihopa!
>
> Jag håller på att skapa ett 3 el flera i rad spel i COCOS2D och skulle
> behöva er hjälp med att lösa en sak.
>
> Koden nedan visar metoden som hittar 3 el flera i rad.
>
> Det jag behöver er hjälp med är att få metoden att kunna känna igen om
> det tas bort brickor vertikalt och horisontellt samtidigt och om
> minst en av dessa brickor ligger i bägge grupperna. då ska metoden ge
> dubbla poängen på brickan.
>
> tex
>
> om tre brickor tas bort horisontellt och tre brickor tas samtidigt
> bort vertikalt så att dessa 3 brickor bildar ett kors. då ska brickan
> som ligger i mitten av korset eftersom den finns med i både vertikala
> gruppen och den horisontella få dubbla poång
>
> tack i förhand
>
> //Check for matches
> -(void)checkGroups:(bool)firstTime{
> int curHGroup = 0;
> int curVGroup = 0;
> int lastGroup =-1;
>
> NSMutableArray * groupings = [[NSMutableArray alloc]init];
>
> for(int i =0; i< GRID_WIDTHIPAD ; i++)
> {
> for(int j =0; j< GRID_HEIGHTIPAD ; j++)
> {
>
> Bricks * d = (Bricks *)grid[i][j];
> d.disappearing = NO;
> Bricks * l =nil;
> Bricks * t =nil;
>
> if(i>0)
> l = (Bricks *)grid[i-1][j];
> if(j>0)
> t = (Bricks *)grid[i][j-1];
>
> //IF there is a previous brick in the grid we compare the actual
> one with that one. If they
> //are of the same type we add it to that group.
> //If not, we create a new horizontal group and add the brick to
> that one.
>
> if(l && l.bricksType == d.bricksType){
>
> [[groupings objectAtIndex:l.curHGroup] addObject:grid[i][j]];
> grid[i][j].curHGroup = l.curHGroup;
>
> }
> else {
>
> curHGroup = lastGroup +1;
> NSMutableSet * group = [[NSMutableSet alloc]init];
> [groupings addObject:group];
> [group release];
> [[groupings objectAtIndex:curHGroup] addObject:grid[i][j]];
> grid[i][j].curHGroup = curHGroup;
> lastGroup = curHGroup;
>
> }
>
> //The same for grouping vertically
>
> if(t && t.bricksType == d.bricksType){
>
> [[groupings objectAtIndex:t.curVGroup] addObject:grid[i][j]];
> grid[i][j].curVGroup = t.curVGroup;
>
> }
> else{
>
> curVGroup = lastGroup+1;
> NSMutableSet * group2 = [[NSMutableSet alloc]init];
> [groupings addObject:group2];
> [group2 release];
> [[groupings objectAtIndex:curVGroup] addObject:grid[i][j]];
> grid[i][j].curVGroup = curVGroup;
> lastGroup = curVGroup;
>
> }
> }
> }
>
> BOOL moveBricks = NO;
> for (NSMutableSet * n in groupings)
> {
> if([n count]>=3)
> {
> ChangeAmountofBricksCountTime = 1.5;
> //CCLOG(@"ChangeAmountofBricksCountTime %d",
> ChangeAmountofBricksCountTime % 60);
>
> for(Bricks * c in n)
> {
> self.allowTouch = NO;
> c.disappearing = YES;
> moveBricks = YES;
> [c.mySprite setOpacity:0];
> [c.mySprite runAction:[CCFadeOut actionWithDuration:0.5]];
>
> CCSprite *Starsprite = [CCSprite spriteWithFile:@"star.png"];
> CCSprite *Scoresprite100 = [CCSprite spriteWithFile:@"100.png"];
> CCSprite *Scoresprite200 = [CCSprite spriteWithFile:@"200.png"];
> CCSprite *Scoresprite300 = [CCSprite spriteWithFile:@"300.png"];
> CCSprite *Scoresprite400 = [CCSprite spriteWithFile:@"400.png"];
> CCSprite *Scoresprite500 = [CCSprite spriteWithFile:@"500.png"];
> CCSprite *Scoresprite600 = [CCSprite spriteWithFile:@"600.png"];
> //CCSprite *X2 = [CCSprite spriteWithFile:@"x2.png"];
> CCSprite *X3 = [CCSprite spriteWithFile:@"x3.png"];
> CCSprite *X4 = [CCSprite spriteWithFile:@"x4.png"];
> //CCSprite *X5 = [CCSprite spriteWithFile:@"x5.png"];
>
> //CGSize screenSize = [CCDirector sharedDirector].winSize;
>
> //Adding a VFX in form of a star when bricks are being removed
> from the gameboard
> if(c.mySprite.opacity == 0){
> Starsprite.position = c.mySprite.position;
>
> if ([[GameManager sharedGameManager] isSoundEffectsON] == NO) {
> STOPSOUNDEFFECT(starSound); // SFX are OFF
> }
> else{
> starSound = PLAYSOUNDEFFECT(Star);
> }
>
> //Showing the score of the bricks
> if(c.bricksType == 0 || c.bricksType == 6 || c.bricksType == 12
> || c.bricksType == 18){
> Scoresprite100.position = Starsprite.position;
> Scoresprite100.position = CGPointMake(Scoresprite100.position.x
> +18, Scoresprite100.position.y +18);
> [self addChild:Scoresprite100 z:0];
> id ScUpSco = [CCScaleTo actionWithDuration:0.3 scale:1.0 ];
> id ScDownSco = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeSco = [CCFadeOut actionWithDuration:0.5];
> [Scoresprite100 runAction:[CCSequence actions:ScUpSco,
> ScDownSco, FadeSco, nil] ];
>
> score += (100 * [n count]);
>
> //Tripple the Score if there are 5 bricks in a row
> if([n count]== 5){
>
> X3.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X3 z:1];
> id ScUpX3 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX3 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX3 = [CCFadeOut actionWithDuration:0.5];
> [X3 runAction:[CCSequence actions:ScUpX3, ScDownX3, FadeX3,
> nil] ];
>
> score += 1500;
> }
>
> //Quatripple the Score if there are 5 bricks in a row
> if([n count]== 6){
>
> X4.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X4 z:1];
> id ScUpX4 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX4 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX4 = [CCFadeOut actionWithDuration:0.5];
> [X4 runAction:[CCSequence actions:ScUpX4, ScDownX4, FadeX4,
> nil] ];
>
> score += 2000;
> }
>
>
> }
> //else if(c.bricksType == 1){
> else if(c.bricksType == 1 || c.bricksType == 7 || c.bricksType ==
> 13 || c.bricksType == 19){
> Scoresprite200.position = Starsprite.position;
> Scoresprite200.position = CGPointMake(Scoresprite200.position.x
> +18, Scoresprite200.position.y +18);
> [self addChild:Scoresprite200 z:0];
> id ScUpSco = [CCScaleTo actionWithDuration:0.3 scale:1.0 ];
> id ScDownSco = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeSco = [CCFadeOut actionWithDuration:0.5];
> [Scoresprite200 runAction:[CCSequence actions:ScUpSco,
> ScDownSco, FadeSco, nil] ];
>
> score += (200 * [n count]);
>
> //Tripple the Score if there are 5 bricks in a row
> if([n count]== 5){
>
> X3.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X3 z:1];
> id ScUpX3 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX3 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX3 = [CCFadeOut actionWithDuration:0.5];
> [X3 runAction:[CCSequence actions:ScUpX3, ScDownX3, FadeX3,
> nil] ];
>
> score += 3000;
> }
>
> //Quatripple the Score if there are 5 bricks in a row
> if([n count]== 6){
>
> X4.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X4 z:1];
> id ScUpX4 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX4 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX4 = [CCFadeOut actionWithDuration:0.5];
> [X4 runAction:[CCSequence actions:ScUpX4, ScDownX4, FadeX4,
> nil] ];
>
> score += 4000;
> }
>
> }
> //else if(c.bricksType == 2){
> else if(c.bricksType == 2 || c.bricksType == 8 || c.bricksType ==
> 14){
> Scoresprite300.position = Starsprite.position;
> Scoresprite300.position = CGPointMake(Scoresprite300.position.x
> +18, Scoresprite300.position.y +18);
> [self addChild:Scoresprite300 z:0];
> id ScUpSco = [CCScaleTo actionWithDuration:0.3 scale:1.0 ];
> id ScDownSco = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeSco = [CCFadeOut actionWithDuration:0.5];
> [Scoresprite300 runAction:[CCSequence actions:ScUpSco,
> ScDownSco, FadeSco, nil] ];
>
> score += (300 * [n count]);
>
> //Tripple the Score if there are 5 bricks in a row
> if([n count]== 5){
>
> X3.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X3 z:1];
> id ScUpX3 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX3 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX3 = [CCFadeOut actionWithDuration:0.5];
> [X3 runAction:[CCSequence actions:ScUpX3, ScDownX3, FadeX3,
> nil] ];
>
> score += 4500;
> }
>
> //Quatripple the Score if there are 5 bricks in a row
> if([n count]== 6){
>
> X4.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X4 z:1];
> id ScUpX4 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX4 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX4 = [CCFadeOut actionWithDuration:0.5];
> [X4 runAction:[CCSequence actions:ScUpX4, ScDownX4, FadeX4,
> nil] ];
>
> score += 6000;
> }
> }
> //else if(c.bricksType == 3){
> else if(c.bricksType == 3 || c.bricksType == 9|| c.bricksType ==
> 15){
> Scoresprite400.position = Starsprite.position;
> Scoresprite400.position = CGPointMake(Scoresprite400.position.x
> +18, Scoresprite400.position.y +18);
> [self addChild:Scoresprite400 z:0];
> id ScUpSco = [CCScaleTo actionWithDuration:0.3 scale:1.0 ];
> id ScDownSco = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeSco = [CCFadeOut actionWithDuration:0.5];
> [Scoresprite400 runAction:[CCSequence actions:ScUpSco,
> ScDownSco, FadeSco, nil] ];
>
> score += (400 * [n count]);
>
> //Tripple the Score if there are 5 bricks in a row
> if([n count]== 5){
>
> X3.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X3 z:1];
> id ScUpX3 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX3 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX3 = [CCFadeOut actionWithDuration:0.5];
> [X3 runAction:[CCSequence actions:ScUpX3, ScDownX3, FadeX3,
> nil] ];
>
> score += 6000;
> }
>
> //Quatripple the Score if there are 5 bricks in a row
> if([n count]== 6){
>
> X4.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X4 z:1];
> id ScUpX4 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX4 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX4 = [CCFadeOut actionWithDuration:0.5];
> [X4 runAction:[CCSequence actions:ScUpX4, ScDownX4, FadeX4,
> nil] ];
>
> score += 8000;
> }
> }
> //else if(c.bricksType == 4){
> else if(c.bricksType == 4 || c.bricksType == 10 || c.bricksType
> == 16){
> Scoresprite500.position = Starsprite.position;
> Scoresprite500.position = CGPointMake(Scoresprite500.position.x
> +18, Scoresprite500.position.y +18);
> [self addChild:Scoresprite500 z:0];
> id ScUpSco = [CCScaleTo actionWithDuration:0.3 scale:1.0 ];
> id ScDownSco = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeSco = [CCFadeOut actionWithDuration:0.5];
> [Scoresprite500 runAction:[CCSequence actions:ScUpSco,
> ScDownSco, FadeSco, nil] ];
>
> score += (500 * [n count]);
>
> //Tripple the Score if there are 5 bricks in a row
> if([n count]== 5){
>
> X3.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X3 z:1];
> id ScUpX3 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX3 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX3 = [CCFadeOut actionWithDuration:0.5];
> [X3 runAction:[CCSequence actions:ScUpX3, ScDownX3, FadeX3,
> nil] ];
>
> score += 7500;
> }
>
> //Quatripple the Score if there are 5 bricks in a row
> if([n count]== 6){
>
> X4.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X4 z:1];
> id ScUpX4 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX4 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX4 = [CCFadeOut actionWithDuration:0.5];
> [X4 runAction:[CCSequence actions:ScUpX4, ScDownX4, FadeX4,
> nil] ];
>
> score += 10000;
> }
> }
> //else if(c.bricksType == 5){
> else if(c.bricksType == 5 || c.bricksType == 11 || c.bricksType
> == 17){
> Scoresprite600.position = Starsprite.position;
> Scoresprite600.position = CGPointMake(Scoresprite600.position.x
> +18, Scoresprite600.position.y +18);
> [self addChild:Scoresprite600 z:0];
> id ScUpSco = [CCScaleTo actionWithDuration:0.3 scale:1.0 ];
> id ScDownSco = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeSco = [CCFadeOut actionWithDuration:0.5];
> [Scoresprite600 runAction:[CCSequence actions:ScUpSco,
> ScDownSco, FadeSco, nil] ];
>
> score += (600 * [n count]);
>
> //Tripple the Score if there are 5 bricks in a row
> if([n count]== 5){
>
> X3.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X3 z:1];
> id ScUpX3 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX3 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX3 = [CCFadeOut actionWithDuration:0.5];
> [X3 runAction:[CCSequence actions:ScUpX3, ScDownX3, FadeX3,
> nil] ];
>
> score += 9000;
> }
>
> //Quatripple the Score if there are 5 bricks in a row
> if([n count]== 6){
>
> X4.position = CGPointMake(Starsprite.position.x+35,
> Starsprite.position.y+35);
> [self addChild:X4 z:1];
> id ScUpX4 = [CCScaleTo actionWithDuration:0.3 scale:0.5 ];
> id ScDownX4 = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id FadeX4 = [CCFadeOut actionWithDuration:0.5];
> [X4 runAction:[CCSequence actions:ScUpX4, ScDownX4, FadeX4,
> nil] ];
>
> score += 12000;
> }
> }
>
> }
>
> [self addChild:Starsprite z:0];
> id ScUp = [CCScaleTo actionWithDuration:0.3 scale:1.0 ];
> id ScDown = [CCScaleTo actionWithDuration:0.3 scale:0 ];
> id Fade = [CCFadeOut actionWithDuration:0.5];
> [Starsprite runAction:[CCSequence actions:ScUp, ScDown, Fade,
> nil] ];
>
> //Set the current score
> [scorelabel setString:[NSString stringWithFormat:@"%d",score]];
> [[GameManager sharedGameManager] setPScore:score];
>
> //If the current score is larger or equal to the highscore then
> change
> //the highscore to be equal to the players score
> if(score >= highscore){
> [[GameManager sharedGameManager] SetHScore: highscore];
> highscore = score;
> [highscorelabel setString:[NSString
> stringWithFormat:@"%d",highscore]];
>
> }
> //Else let the highscore be
> else if(score < highscore){
> [[GameManager sharedGameManager] SetHScore: highscore];
> highscore = highscore;
> [highscorelabel setString:[NSString
> stringWithFormat:@"%d",highscore]];
>
> }
> }
>
> //Change the level if the score has reached a specific sum
> [self addScore];
>
>
> //Checking if the player has used any of the rubies when reaching a
> specific score
> if((score >= 3000000) || (score >= 5000000) || (score >= 7000000)){
> [self CheckIfThereAreAnyRubiesUponReachingThisScore];
> }
>
> }
> }
>
> //We are done with the groupings array, release it.
> [groupings release];
>
> if(moveBricks)
> {
> [self schedule:@selector(moveBricksDown) interval:0.5];
> [self schedule:@selector(GridRefill) interval:0.000001];
> }
> else
> {
> self.allowTouch = YES;
> self.allowTouchRuby = YES;
> CCLOG(@"self.allowTouch = YES");
> [self schedule:@selector(GridRefill) interval:0.000001];
> }
> }

Christos

unread,
Dec 5, 2011, 1:00:52 AM12/5/11
to Sweden iPhone Development
Hej Erik!

Tack för tipsen. :)

Jag håller med att det blev mycket kod och jag vet att de flesta inte
har tid.

Problemet är dock att hela den metoden behövs om jag ska kunna få
hjälp med mitt problem.

Då var det att antingen posta hela metoden från början eller skriva
ut den senare när förfrågan om det kommer upp pga att jag inte tagit
med den från början.
I vilket fall som så skulle det ha blivit 13 sidors kod.

Hade jag kunnat ta bort delar ut den så skulle jag ha gjort det utan
tvekan :)

När det gäller mer specifika frågor så vet jag inte hur mer speficik
jag kan vara gällande det jag försökter åstadkomma.

Jag ska fundera lite och se om jag kan formulera om frågan.

återkommer!

> ...
>
> read more »

Jack Nutting

unread,
Dec 5, 2011, 2:53:42 AM12/5/11
to iphone...@googlegroups.com, Sweden iPhone Development
Hej Christos,

Jag föreslår att du läser denna sida, som förklarar hur man "bäst" ställer tekniska frågor:

http://catb.org/~esr/faqs/smart-questions.html

Det är ju inte så att du har dåliga frågor, det är bara att du lägger i så mycket sammanhang att kärnfrågan är ibland svårt att se. Ett tips är att försöka reducera problemet. Om man bygger upp sin kod i mindre metoder t ex, då blir det oftast lättare att testa och isolera problem, samt att ställa frågor om det. Om man har en del funktioner som man vet funkar korrekt, då kan man posta frågan med bara problemområdet.

Detta är inte menat för att tillrättavisa dig, utan att hjälpa dig komma med frågor som är lättare för andra att sätta sig i, som leder till att du lättare får svar.

Ska nämna att en bra sidoeffekt av att refactor koden i mera hanterbar chunks är att man ofta hittar problemen själv under tiden, eftersom man är tvungen att tänka till ett varv. Så är det för mig i alla fall. De flesta frågeställningar jag påbörjar på en lista eller på stackexchange lämnar aldrig min dator, eftersom jag hittar felen medan jag förklarar problemen.

// jack

Christos

unread,
Dec 5, 2011, 3:00:15 AM12/5/11
to Sweden iPhone Development
Hej Jack! :)

ingen fara.

jag uppskattar som sagt all hjälp jag får av er

det är så man lär sig. :)

jag kollar på länken och så återkommer jag men en bättre formulering
på frågan ;)

ha en underbar dag allihopa

> ...
>
> läs mer »- Dölj citerad text -
>
> - Visa citerad text -

Martin Fors

unread,
Dec 5, 2011, 8:38:33 PM12/5/11
to iphone...@googlegroups.com
Mitt bästa tips e att slänga all kod som du copypastat och försöka
sätta dig in i det du gör innan du ställer frågor här.
http://www.cocos2d-iphone.org/forum/topic/21631

/martin

Elektroniskt brev ifrån iPad.

Christos

unread,
Dec 5, 2011, 11:17:00 PM12/5/11
to Sweden iPhone Development
har försökt ett tag nu att lösa detta problem på egen hand innan jag
valde att ställa frågan här

får försöka igen.

tack för tipsen. :)

ha en underbar dag!


On Dec 6, 2:38 am, Martin Fors <martin.f...@gmail.com> wrote:
> Mitt bästa tips e att slänga all kod som du copypastat och försöka

> sätta dig in i det du gör innan du ställer frågor här.http://www.cocos2d-iphone.org/forum/topic/21631

> ...
>
> read more »

Martin Öhman

unread,
Dec 7, 2011, 2:55:33 AM12/7/11
to iphone...@googlegroups.com
Ett annat tips är att läsa den här sidan och titta på koden du klippte in och se om du kan göra några ändringar.

http://en.wikipedia.org/wiki/Decomposition_(computer_science)

Vänliga hälsningar,
Martin Öhman

Apping
Pilefeltsgatan 73
302 50 Halmstad

tlf: 070-6482598
mail: mar...@apping.se
www: www.apping.se

Reply all
Reply to author
Forward
0 new messages