Help... My code doesn't work - but it should

A monster walks along a corridor getting whacked by various weapons.

The problem is that I insert the monster into a display group and once i’s moved it becomes impossible to hittest it. I must have missed something.

[lua]local physics = require(“physics”);
physics.start()
physics.setGravity(0,0);
physics.setDrawMode(“hybrid”);

– define objects

local monster1;
local weapon1;
local weapon1Sensor;

– define functions

local updatePhysicsBody;
local weaponHitmonster;
local weapon1DisplayGroup = display.newGroup();

require “sprite”


– Functions

– collision function

local function weaponHitmonster( self, event )
if(event.phase==“began”) then
print(“hit”);
end
end

function updatePhysicsBody(event)
weapon1Sensor.y = event.sprite.height -25;
end

local weapon = sprite.newSpriteSheetFromData(“weapon.png”, require(“weapon”).getSpriteSheetData());

local spriteSet1 = sprite.newSpriteSet(weapon,1,4)

sprite.add(spriteSet1,“upDown”,1,4,2000,0)
weapon1 = sprite.newSprite(spriteSet1)
weapon1DisplayGroup:insert(weapon1);

weapon1.x = 100;
weapon1.y = 140;

weapon1:prepare(“upDown”);
weapon1:play();
weapon1:addEventListener(“sprite”, updatePhysicsBody);
weapon1Sensor = display.newRect(weapon1.x-25,weapon1.y-20,50,60);
weapon1Sensor.alpha = .5;
physics.addBody(weapon1Sensor, “dynamic”)
weapon1Sensor.collision = weaponHitmonster;
weapon1Sensor:addEventListener(“collision”, weapon1Sensor);

monster = display.newRect(0,200,50,50);

physics.addBody(monster, “dynamic”, { isSensor = true} )
local monsterDisplayGroup = display.newGroup();
monsterDisplayGroup:insert(monster);

transition.to(monsterDisplayGroup, {x=500, time=10000});[/lua]

[import]uid: 55068 topic_id: 15018 reply_id: 315018[/import]

Physics bodies all need to be in the same group. [import]uid: 84637 topic_id: 15018 reply_id: 55504[/import]

Thanks Danny - would be interested in hearing any workarounds people have for this.

Isn’t it a common thing to want to hittest an object in a group with an object in another group?

Thanks

Tom [import]uid: 55068 topic_id: 15018 reply_id: 55548[/import]

I have items from two groups collide all the time. What I have found is that if you assign x and y values to your groups, however, the collision system fails between those groups. Seems the collision detection relies on checking object coordinates against each other and when you assign group x,y values you localize the object coordinates to different origins, so according to the system the objects do not overlap.

Might that be what’s happening here? [import]uid: 64596 topic_id: 15018 reply_id: 55558[/import]

–double post-- :stuck_out_tongue: [import]uid: 64596 topic_id: 15018 reply_id: 55559[/import]

Wow, shane, that explains some of the oddity I’ve seen with static body group that moves.

I have dynamic objects that bounce when they hit static object group, but sometimes they simply go through the static object group, and some other times, even when there are no static objects blocking the way, they behave as if they’ve hit a static object.

I suppose this oddity is caused by the static body group that moves. Thank you for sharing your observation. [import]uid: 67217 topic_id: 15018 reply_id: 55577[/import]

Thanks Shane, but I’m not assigning x and y to my groups. It’s when using transitions on groups I cant get them to collide. If you’re managing to use transitions, groups and hittesting together would you mind posting a snippet of code please?

Thanks

Tom [import]uid: 55068 topic_id: 15018 reply_id: 55730[/import]

Sorry Tom, I’ve been doing all my transitions manually by iterating through a table I register my items to.

Maybe someone else has done this? Anyone? [import]uid: 64596 topic_id: 15018 reply_id: 55807[/import]