Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Values being Updated ONLY ON MOUSE UP??? — Gideros Forum

Values being Updated ONLY ON MOUSE UP???

Paulo777Paulo777 Member
edited February 2018 in General questions
Hello Giderians :)
Have you experienced a thing I'm living on? I have a sprite that has the following:
function Ship:onMouseUp(event)
 
	self:removeEventListener(Event.ENTER_FRAME, self.onShipFire, self)
end
 
function Ship:onMouseDown(event)
 
	if self:hitTestPoint(event.x, event.y) then
		self:addEventListener(Event.ENTER_FRAME, self.onShipFire, self)
	end
end
 
function Ship:onShipFire()
 
	local x, y = self:getPosition()
	x = x + self:getWidth()/2
 
	self.shot = Fire.new("objects/ship/fire_1.png", -30, 0, x, y)
	self.shot:onFire() 
end
on the other sprite I have:
if self.ship.shot:collidesWith(self) then
	self.damaged += 1
	print(self.damaged)
end
What i want: When I mouse down, is for the valuess to update, but it's not what's going on, the value just updates AFTER I MOUSE UP. I have no any idea why It happens I don't find any argument why It's happening.

Help me please.
Tagged:

Comments

  • antixantix Member
    edited February 2018
    In your function onMouseDown you are checking against event.x and event.y. These do not exist. You need to use event.touch.x and event.touch.y

    http://g2d.atwebpages.com/reference.html?class=Sprite&method=TOUCHES_BEGIN
  • Omg! Thanks @antix for your light...
    This really passed by me unoticed... Getting home I bring you news about.
  • hgy29hgy29 Maintainer
    @antix, you are talking about TOUCHES events, while the OP seems to talk about MOUSE. In mouse events, event.x and event.y do exist!

    Likes: oleg, Apollo14, antix

    +1 -1 (+3 / -0 )Share on Facebook
  • @hgy29 initially I doubted about it otherwise the compiler might throw error... But a thing I noticed is that when I 'mouse_down' It triggers lots of shots and when I get closer to the object It works as expected, but when I move far from the sprite, it stops working, and only when I mouse_down/mouse_up it counts. have you got any ideia about it?
  • hgy29hgy29 Maintainer
    I don't see anything wrong in your code, maybe the problem comes from some other part of the code ?
  • Paulo777Paulo777 Member
    edited February 2018
    if self.ship.shot:collidesWith(self) then
    	self.damaged += 1
    	print(self.damaged)
    end
    this block of code is executed inside a "ENTER_FRAME" event in the "gear" sprite.

    this "gear" sprite has a "sprite" as parameter
    function Gear:init(sprite)
     
    self.ship = sprite
     
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
     
    function Gear.onEnterFrame(self)
     
    if self.ship.shot:collidesWith(self) then
    	self.damaged += 1
    	print(self.damaged)
    end
     
    end
    in the "level" sprite I have:
     
    -- enter frame event --
     
    gears = {
    Gear.new(ship)
    Gear.new(ship)
    Gear.new(ship)
    }
     
    if self.ship.shot:collidesWith(self) then
        --do something --
    end
    @hgy29
  • olegoleg Member
    edited February 2018
    It's a bad idea to create an ENTER_FRAME listener every time a button is pressed
    function Ship:onMouseUp(event)
     self.flag_fire = false
     
    end
     
    function Ship:onMouseDown(event)
     
    	if self:hitTestPoint(event.x, event.y) then
    		self.flag_fire = true
    	end
    end
     
     
     
    function Ship:onShipFire(self)
     if self.flag_fire == true then
    	local x, y = self:getPosition()
    	x = x + self:getWidth()/2
     
    	self.shot = Fire.new("objects/ship/fire_1.png", -30, 0, x, y)
    	self.shot:onFire() 
    end
    end
     
    self:addEventListener(Event.ENTER_FRAME, self.onShipFire, self)
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • Paulo777Paulo777 Member
    edited February 2018
    OMG!! This is really mightily helpful!! :) as I'm still a beginner, I need to study deeply I have a vague understanding over events and sprite relationships I feel like I'm still crawling. I'll test it when I get home and any news I tell y'all! Thank you @oleg for some light.
  • @oleg this command:
    self:addEventListener(Event.ENTER_FRAME, self.onShipFire, self)
    must be inside the 'init' ?
  • @Paulo777 so put into init
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • Paulo777Paulo777 Member
    edited February 2018
    Hello! Nice! It really worked as expected. I verify if flag is true... ok... but... it is only incrementing when I get closer to the object... when I get farther it stops incrementing... @oleg
  • @Paulo777

    sorry i do not understand what you mean
    function Ship:onMouseDown(event)
     
    	--if self:hitTestPoint(event.x, event.y) then
    		self.flag_fire = true
    	--end
    end
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • Paulo777Paulo777 Member
    edited February 2018
    oleg said:

    @Paulo777

    sorry i do not understand what you mean

    function Ship:onMouseDown(event)
     
    	--if self:hitTestPoint(event.x, event.y) then
    		self.flag_fire = true
    	--end
    end
    @oleg initially it means that the ship only has to fire when the player press on the ship

    doing more tests I figured out on collision detection function the values I change influences the distance of collision
    function Fire.collidesWith(self, sprite2)
     
    	local x,y,w,h = self.fire:getBounds(stage)
    	local x2,y2,w2,h2 = sprite2:getBounds(stage)
    	--if I set y2 + (something) it influences the distance of collision
     
    	-- self bottom < other sprite top
    	if y + h < y2 then
    		return false
    	end
    	-- self top > other sprite bottom
    	if y > y2 + h2 then
    		return false
    	end
    	-- self left > other sprite right
    	if x > x2 + w2 then
    		return false
    	end
    	-- self right < other sprite left
    	if x + w < x2 then
    		return false
    	end
     
    	--print('self bounds:',x,y,w,h,' sprite2 bounds:',x2,y2,w2,h2)
    	return true
    end
  • Paulo777Paulo777 Member
    edited February 2018
    Hello everybody!
    I got a solution. I realized that... I don't know how I can explain the behaviour, mouse_down event I saw that the fire went to its destination and did what was expected it to do, but the fire would shoot once every time I 'mouse_down'. Then I created a
    self.frame = 0
    and inside self.flag = true, make it to fire every x frames then I set a value higher than 1 per 60.
    if self.fire_flag = true then
    	self.frame += 2
    	if self.frame > 8 then
    		-- fire --
    	end
    end
    if I decrease the frame, it only works if I touch the other sprite, otherwise the fire doesn't works

    This way I configured the fire according to my needs

    I suppose that as the frames quantity is too high, there must not have time for the fire to conclude, maybe only one shoot works, and as I was shooting one behind the other, the event leave to exist... I don't know too much technically, if someone could explain why It happens is very appreciated.
Sign In or Register to comment.