0

I am developing an android game on Eclipse in which a sprite is jumping on several platforms to get to the top but I am having difficulties. Here is my case: I have the character sprite (I'll name this as Sprite1 in this case) that moves and I am creating another 3 sprites(the platforms) at random positions when my app starts. The platforms are successfully created when the app starts. But the collision between them and Sprite1 is not detected. I tried many solutions and I am stuck for a couple of days now. Any help or suggestion will be appreciated.

I tried making rects when the platforms are created then using the rects to detecting collisions but they still don't collide. I use Java language. :)

I use this to create the sprites and detect collisions:

Creating platform sprites (this is how i randomly spawn platforms, if there are mistakes here, i'm sure i just copied wrong because the creating or platform sprites works correctly.):

public void addRandomPlatform(){
    Random rand = new Random();
    randPlat = CCSprite.sprite("platform2.png");
    randPlat.setPosition(actualX, actualY);
        actualY -= (int)(randPlat.getContentSize().height*4);
        addChild(randPlat,2);
}

Then I just call this method when my app starts.

Detecting Collision:

playerRect = CGRect.make(player.getPosition().x - (player.getContentSize().width /   4.0f),//- (player.getContentSize().width / 2.0f),
                     player.getPosition().y - (player.getContentSize().height / 2.0f),
                 player.getContentSize().width/3.0f,
                 player.getContentSize().height/8.0f);

if(CGRect.intersects(playerRect, randPlat))
    {
       *my codes for stopping the jump*
}

I hope this is clear. Thanks in advance!

2 Answers2

0

It looks like the randRect is a CCSprite, and not a CGRect. You should create a CGRect(of the random platform) the same way you created the playerRect.

Also, why do you divide the player's width/height by respectively 3 and 8? I don't think that's what you want, and it causes the playerRect to be only a thirth of the width, and 1/8 of the height of the actual sprite.

fifarunnerr
  • 617
  • 4
  • 12
  • I tried creating rects of the platforms after they are randomly created but the collision was still not detected. I divided the playerRect's height by 8 because I only want the lower part of the player (the foot of the character) to be detected. I had a case when the player is going down (from a jump) and his head part intersected the platform, it stops jumping. That was fixed by making the rect's height smaller. – user3194348 Jan 15 '14 at 18:32
  • Try logging the x, y, width and height of both rects to Logcat. If that doesn't help you to find the issue, post the (logged) values of both rects when they intersect(on the View), but not in the code. – fifarunnerr Jan 15 '14 at 18:38
  • I tried logging the x,y,width and height of the platforms and rects. The x and y are the same but regarding the height and width, I can't determine if their sizes are the same because i use platform.getScaleX() and getScaleY() when getting sprites sizes while rect.size.width and .size.height for rects. – user3194348 Jan 15 '14 at 19:01
0
    @Override
    public boolean ccTouchesEnded(MotionEvent event) {
  CGPoint point =  ranSprite.getPosition();// THE POSITION MUST BE YOUR RANDOM SPRITES POSITIONS

        List<CCNode> childsList = this.getChildren();

        for(CCNode childSprite : childsList){

            if(childSprite.getTag() == Constants.PLAYER_TAG   && childSprite.getBoundingBox().contains(point.x, point.y)){


                // Do what ever you want

}
}
Rama
  • 1,156
  • 1
  • 7
  • 13