3
\$\begingroup\$

I'm creating a RPG game, I need to create a monster respawn area like the Pokemon games where if I walk through the long grass areas a Pokemon can appear. I already created the battle scene, I've seen examples of tile collision with solid objects but I don't know how create a collision action with passable objects.

\$\endgroup\$
4
\$\begingroup\$

I suppose you could call it a collision, but it's more like an intersection. You can create a trigger zone that activates when the player enters it. A simple way of doing that would just create a list of these zones, then check to see if the player is inside them every frame or when the player moves. A zone is just two points, a min and a max. Then you can check when something is inside like so:

public boolean contains(float x, float z) {
    return (x >= minX && x < maxX &&
        z >= minZ && z < maxZ);
}
\$\endgroup\$
  • \$\begingroup\$ actually i'm populating the tiles of map that have the property("monsterRespawn","true") with rectangle objects, my idea is when the character step over the tile where any of this objects exist, trigger a random action of can be attacked or not, but i'dont know if is good idea and how call a certain action when the player is over the tile \$\endgroup\$ – dakairus Jul 20 '12 at 18:28
0
\$\begingroup\$

Finally i got how create the respawn zone, i create 2 arrays of tmxmap objects and extract is properties in the example i create wall(unwalkable zone) and isMonsterRespawn zone, where if the player collide with that zone certain action happens, in my example case the respawn zone change the color tile. There i can set an action that cant send my character to the battle scene with a randomly chance:

private void createUnwalkableObjects(TMXTiledMap map,final AnimatedSprite pl) {

 for(final TMXObjectGroup group: this.mTMXTiledMap.getTMXObjectGroups()) {

   if(group.getTMXObjectGroupProperties().containsTMXProperty("wall", "true")){
             // This is our "wall" layer. Create the boxes from it
   for(final TMXObject object : group.getTMXObjects()) {

                final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
                rect.setVisible(false);
                final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
                PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
                mScene.attachChild(rect);
             }
         }if(group.getTMXObjectGroupProperties().containsTMXProperty("IsMonsterSpawnArea", "true")){
             // This is our "wall" layer. Create the boxes from it
             for(final TMXObject object : group.getTMXObjects()) {
                final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
    rect.setVisible(true);
    mScene.attachChild(rect);
    mScene.registerUpdateHandler(new IUpdateHandler() {
                    @Override
                    public void reset() {

}

                    @Override
                    public void onUpdate(final float pSecondsElapsed) {
                        if(pl.collidesWith(rect)){
//here the action when the player collides the zone
                            rect.setColor(1, 0, 1);}else{
                                rect.setColor(1, 0, 0);
                            }   
                        }

                });

             }
         }
     }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.