Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How to make my mouseUp event work only when I click on the enemy? — Gideros Forum

How to make my mouseUp event work only when I click on the enemy?

AxlFlameAxlFlame Member
edited January 2013 in Game & application design
Hi,
I'm creating a game where basically if the player clicks on an enemy, the enemy is destroyed. My problem here is, the way I coded, regardless of where the player clicks, the enemy is destroyed and I wanted that to happen only when the player clicks on the enemy. How can I make that happen?
Thanks in advance!
This is the enemy class (bDobstaculos.lua)
bDobstaculos = gideros.class(Bitmap);
 
 
function bDobstaculos:init(text, xPos, yPos, b2World)
	self:setAnchorPoint(0.5, 0.5);
	self.body = b2World:createBody{
				type = b2.STATIC_BODY, 
				position = {x = 400, y = 140},
				fixedRotation = true,
				allowSleep = false;
				active = true,
				gravityScale = 1
			};
	self.body.name = "spike";
	local shapeSpike = b2.PolygonShape.new()
	shapeSpike:setAsBox(25, 40)
	self.fixture = self.body:createFixture{shape = shapeSpike,
				friction = 0,
				restitution = 0,
				density = 30,
				isSensor = true
				};
	self:setPosition(xPos, yPos-self:getHeight()*0.5);
end
 
function bDobstaculos:addListeners()
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
	self:addEventListener(Event.MOUSE_UP, self.apagaInimigo, self);
end
 
function bDobstaculos:removeListeners()
	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
end
 
function bDobstaculos:onEnterFrame()
	self.body:setPosition(self:getPosition());
	local b2World = self.moveMe(self);
	if self:getX() < -50 then
		self.fixture = nil;
		b2World:destroyBody(self.body);
		self.body = nil;
		self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
		self:getParent():removeChild(self);
	end
end
 
function bDobstaculos:apagaInimigo()
	self.fixture = nil;
	b2World:destroyBody(self.body);
	self.body = nil;
	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
	self:getParent():removeChild(self);	
end

Comments

  • edited January 2013
    1. In function bDobstaculos:apagaInimigo(), using hitTestPoint() to check if mouse event is in bound of enemy sprite:
    http://www.giderosmobile.com/documentation/reference_manual.html#Sprite:hitTestPoint

    if enemySprite:hitTestPoint(event.x, event.y) then
    --destroy enemy
    end

    2.self:getParent():removeChild(self)

    you should use:

    self:removeFromParent()

    Likes: AxlFlame

    +1 -1 (+1 / -0 )Share on Facebook
  • @AxlFlame,

    if you want to really make the app 100% proper, then you will have to first check that your touch started on the enemy and the touch ended on the enemy. It might not be important in your app, but what happens if the player start the touch with the friendly unit and then ends it on the enemy? If that is important to you, you might want to also listen for Mouse_Down, not just mouse_up.

    Likes: AxlFlame

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks for the answers, guys! But I have a problem with the hitTestPoint function.
    See, this bDobstaculos.lua is called inside my main game function to create the enemy, but the created enemy is local, so I changed it to global and the code inside the apagaInimigo() is like this:
    function bDobstaculos:apagaInimigo()
    	if enemy:hitTestPoint(Event.x, Event.y) then
    		self.fixture = nil;
    		b2World:destroyBody(self.body);
    		self.body = nil;
    		self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
    		self:getParent():removeChild(self);	
    	end
    end
    Where enemy is the name for the created enemy sprite on my main game. (Since it's global, I tried calling it with _G.enemy, but it didn't work.
    The point is that this code is giving me this error:

    bad argument #1 to 'hitTestPoint' (number expected, got nil)

    Why isn't it getting the x and probably the y? Am I on the right track, as least?

    @OzApps
    I haven't actually thought about that! It sure is important for me that the click starts and ends within the enemy sprite, but I thought it would be best for now to focus on making the MOUSE_UP work, since even that isn't and I didn't quite grasped the idea of the discussion you pointed out...
  • BJGBJG Member
    edited February 2013 Accepted Answer
    Have you defined "Event.x" and "Event.y"...?

    Check out the "Drag Me" example in the demos. The usual syntax is more like...
    local function onMouseDown(self, event)
    	if self:hitTestPoint(event.x, event.y) then

    Likes: AxlFlame

    +1 -1 (+1 / -0 )Share on Facebook
  • AxlFlameAxlFlame Member
    edited February 2013
    Yeah, I was about to post here that I found out it was a sintax error and the undefined event... I feel so dumb lol

    I did like you said and it finally worked!
    function bDobstaculos:apagaInimigo(event)
    	if enemy:hitTestPoint(event.x, event.y) then
    		self.fixture = nil;
    		b2World:destroyBody(self.body);
    		self.body = nil;
    		self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
    		self:getParent():removeChild(self);	
    	end
    end
    Thanks a lot, guys!
Sign In or Register to comment.