1

I am Creating a game where the cars move on the screen. The cars are generated from 4 sides of the screen moving to the opposite side of the screen.

When I touch a car , the car should move at another speed. I Implemented everything but I am stuck at rect ie. on touch.

I took every generated car in a array, then gave every car a tag value.

This is how my CCtouchbegan()

for (CCSprite targetL1 : _targetsL1)
    {

        System.out.println("tag is targetL1 "+ targetL1.getTag());

        if(targetL1.getTag()==1 ||targetL1.getTag()==3||targetL1.getTag()==4)
        {

            targetL1Rect = CGRect.make(targetL1.getPosition().x - ((winSize.width/8.0f)/ 2.0f),
                     targetL1.getPosition().y - ((winSize.height/10.0f) / 2.0f),
                     (winSize.width/8.0f),
                     (winSize.height/10.0f));


        }else if(targetL1.getTag()==2 ){

         targetL1Rect = CGRect.make(targetL1.getPosition().x - ((winSize.width/4.0f) / 2.0f),
                 targetL1.getPosition().y - ((winSize.height/10.0f) / 2.0f),
                 (winSize.width/4.0f),
                 (winSize.height/10.0f));

        }


        for (CCSprite targetL2 : _targetsL2)
        {


             if(targetL2.getTag()==5 ||targetL2.getTag()==7||targetL2.getTag()==8)
                {

                 targetL2Rect = CGRect.make(targetL2.getPosition().x - ((winSize.width/8.0f)/ 2.0f),
                         targetL2.getPosition().y - ((winSize.height/10.0f) / 2.0f),
                         (winSize.width/8.0f),
                         (winSize.height/10.0f));

                }else if(targetL2.getTag()==6 ){

                     targetL2Rect = CGRect.make(targetL2.getPosition().x - ((winSize.width/4.0f) / 2.0f),
                             targetL2.getPosition().y - ((winSize.height/10.0f) / 2.0f),
                             (winSize.width/4.0f),
                             (winSize.height/10.0f));

                }


             for (CCSprite targetR1 : _targetsR1)
             {

                 if(targetR1.getTag()==9 ||targetR1.getTag()==11||targetR1.getTag()==12)
                    {

                     targetR1Rect = CGRect.make(targetR1.getPosition().x - ((winSize.width/16.0f)/ 2.0f),
                             targetR1.getPosition().y - ((winSize.height/5.0f)/2.0f),
                             (winSize.width/16.0f),
                             (winSize.height/5.0f));


                    }else if(targetR1.getTag()==10 ){

                        targetR1Rect = CGRect.make(targetR1.getPosition().x - ((winSize.width/15.0f)/2.0f),
                                 targetR1.getPosition().y - ((winSize.height/2.5f)/2.0f),
                                 (winSize.width/15.0f),
                                 (winSize.height/2.5f));

                    }



                 for (CCSprite targetR2 : _targetsR2)
                 {



                     if(targetR2.getTag()==13 ||targetR2.getTag()==15||targetR2.getTag()==16)
                        {

                         targetR2Rect = CGRect.make(targetR2.getPosition().x - ((winSize.width/16.0f)/ 2.0f),
                                 targetR2.getPosition().y - ((winSize.height/5.0f)/2.0f),
                                 (winSize.width/16.0f),
                                 (winSize.height/5.0f));



                        }else if(targetR2.getTag()==14 ){

                             targetR2Rect = CGRect.make(targetR2.getPosition().x - ((winSize.width/15.0f)/2.0f),
                                     targetR2.getPosition().y - ((winSize.height/2.5f)/2.0f),
                                     (winSize.width/15.0f),
                                     (winSize.height/2.5f));



                        }




                       if (CGRect.intersects(targetL1Rect, targetL2Rect))
                       {

                           CCDirector.sharedDirector().replaceScene(GameOverLayer.scene("you lose"));

                       }
                       if (CGRect.intersects(targetL1Rect, targetR1Rect))
                       {

                           CCDirector.sharedDirector().replaceScene(GameOverLayer.scene("you lose"));

                       }
                       if (CGRect.intersects(targetL1Rect, targetR2Rect))
                       {

                           CCDirector.sharedDirector().replaceScene(GameOverLayer.scene("you lose"));

                       }
                       if (CGRect.intersects(targetL2Rect, targetR1Rect))
                       {


                           CCDirector.sharedDirector().replaceScene(GameOverLayer.scene("you lose"));
                       }
                       if (CGRect.intersects(targetL2Rect, targetR2Rect))
                       {

                           CCDirector.sharedDirector().replaceScene(GameOverLayer.scene("you lose"));
                       }
                       if (CGRect.intersects(targetR1Rect, targetR2Rect))
                       {

                           CCDirector.sharedDirector().replaceScene(GameOverLayer.scene("you lose"));
                       }

                           // write game over   here

                 }

             }

        }

    }


}

when I touch a car, it works sometime and sometimes it does not work. and sometimes when there are two cars from same side, they both move on single touch.

I am not getting ideas on how to manage the cars touch detect.

Please help me.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Sandeep R
  • 2,284
  • 3
  • 25
  • 51
  • http://stackoverflow.com/questions/17963332/how-to-detect-the-ccsprite-touchevent-getboundingbox-equals-of-cctouches/17964711#17964711 – Akarsh M Apr 08 '14 at 15:12
  • http://stackoverflow.com/questions/14937185/how-to-remove-the-sprite-when-i-touch-on-it-in-cocos2d-android – Akarsh M Apr 08 '14 at 15:13

0 Answers0