Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Question about using under_point? inside an array.
- - By h4y4shi Date 2012-03-01 20:25 Edited 2012-03-01 20:39
Hey guys thanks for the help so far. But I have another problem. I made a tile class and it has a method named under_point? I want to test if the objects are under_point? The problem is that I allowed the user to push alot of objects onto the array. So I want to be able to find out which object is being clicked on and delete or pop that object off of the array whenever a user clicks on it. So far I have the erase button but all it does is delete the [-1] object off of the array until I stop holding down left mouse.

Please help me find out how I can click on an object and then have that specific object popped off of the array.

Here is the source code.

Main.rb http://pastie.org/3499100
Tileclasses.rb http://pastie.org/3499001
Buttonclasses.rb http://pastie.org/3499004
Parent - - By lol_o2 Date 2012-03-02 14:06
.pop method you used deletes the last element from array. You need to test all tiles and use .delete


when 'eraser_selected'
  @water.each{|tile| if tile.under_point?(mouse_x,mouse_y) then @water.delete(tile) end}
  @dirts.each{|tile| if tile.under_point?(mouse_x,mouse_y) then @dirts.delete(tile) end}
  @grasses.each{|tile| if tile.under_point?(mouse_x,mouse_y) then @grasses.delete(tile) end}

To make it simpler, push all tiles in one array (even along with other arrays), like:
when 'grass_selected'
  @grasses.push(tile=GrassTile.new(self))
  @alltiles.push(tile)
  @grasses[-1].tile_position(mouse_x, mouse_y)
Parent - - By h4y4shi Date 2012-03-02 17:47
Im getting an error.

C:/.../tileclasses.rb:22:in >': comparison of Float with nil failed (ArgumentError)
        from C:/Users/h4y4shi/gosu-tile-experiment/tileclasses.rb:22:in
under_point?'
     
        from main.rb:63:in block in update'
        from main.rb:63:in
each'
        from main.rb:63:in update'
        from main.rb:94:in
<main>'

I pasted the code you wrote in the eraser button and I renamed the water.each and water.delete to waters.each and waters.delete because that is the name of the array. Do you know why the error is happening?
Parent - - By arrow Date 2012-03-02 18:21
It's probably because the mouse is over an empty tile, so ruby try to run the under_point? method on a nil object which cause an error
Try this.

when 'eraser_selected'
  @waters.each do |tile|
    ## Skip to next tile if current til is nil
    next if tile.nil?
   
    if tile.under_point?(mouse_x,mouse_y)
      @waters.delete(tile)
    end
  end
 
  @dirts.each do |tile|
    next if tile.nil?
   
    if tile.under_point?(mouse_x,mouse_y)
      @dirts.delete(tile)
    end
  end
 
  @grasses.each do |tile|
    next if tile.nil?
   
    if tile.under_point?(mouse_x,mouse_y)
      @grasses.delete(tile)
    end
  end
Parent - By lol_o2 Date 2012-03-02 19:23
No, it's not that. Error is in this line:
mouse_x > @x and mouse_x < @x + 40 and

tile_position method sets @x_tile instead of @x. That's the problem: the @x variable is nil.
- By h4y4shi Date 2012-03-02 21:46
Thanks a lot it works now.
Up Topic Gosu / Gosu Exchange / Question about using under_point? inside an array.

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill