Click on a image, do complicated stuff.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Click on a image, do complicated stuff.

Post by Raylin »

Hey, guys.
I'm working on a game that involves you clicking on an image and then, when another image is clicked, swap it with another one.
Where things get complicated is when I try to correlate it to a table that holds the towerData.
I'm trying to glean the most efficient way to do it that I will understand...
Anyone have any ideas on how to approach it?

This is for a 24 hour competition by the way. T-minus roughly 8 hours.

Thanks.

EDIT: Let me clarify because I see all of you looking at how silly a question it is.
I need to swap the selected image with the location and data of the next selection.
Something like Bejeweled or a Match-3 puzzle game.
Attachments
House of Cards.love
This thing is a mess.
(37.6 KiB) Downloaded 134 times
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Click on a image, do complicated stuff.

Post by ejmr »

Before making any suggestions I want to make sure I understand exactly what you are trying to do.

On the 'tower' canvas you draw different images based on the values of various indices in 'towerData'. When a player clicks on one of those (or the arrows in the images) you want to swap that image with another one associated with a different index in the 'towerData' table?
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Re: Click on a image, do complicated stuff.

Post by Raylin »

Yes, that is correct.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Click on a image, do complicated stuff.

Post by ejmr »

First of all, my apologies. I am extremely tired and just wrote down the first thing that came to mind, and I know when I look at this after getting some sleep I am going to slap myself for over-complicating this, lol. But I hope this helps in some way.

Code: Select all

-- This table contains the X and Y coordinates of the most recent two
-- mouse clicks.  The two nested tables will contain two keys: 'x' and
-- 'y', each with numeric value indicating where on screen the user
-- clicked the mouse.
local recent_clicks = {}

-- This table represents a map of the tower images.  Each nested table
-- has three keys:
--
-- 1. x
-- 2. y
-- 3. image
--
-- The first two keys represent the coordinate of the upper-left
-- corner of the image's position.  We are making the assumption that
-- each image has the same width and height; that way we can determine
-- whether or not a mouse click occurred inside of image by testing,
-- for example, if the X coordinate of the click is within the bounds
-- of the X coordinate of the image position and its width.
--
-- The image property is the Image object itself that we draw.
local tower_image_map = {
    -- We would fill this out with how the initial map should look.
    -- Or write a function to randomly fill in the image values if we
    -- want the map to look different each time.
}

-- Takes X and Y coordinates of a mouse click and returns the index of
-- tower_image_map that holds the image that click touches.
function find_tower_image_at(x, y)
    for index,tower in ipairs(tower_image_map) do
        if x >= tower.x
            and x <= (tower.x + tower.image:getWidth())
            and y >= tower.y
            and y <= (tower.y + tower.image:getHeight())
        then
            return index
        end
    end
end

-- Every time the user clicks the mouse we store that click inside of
-- the recent_clicks table.
function love.mousepressed(x, y, button)
    table.insert(recent_clicks, {x=x, y=y})

    -- If recent_clicks has more than two values then remove the
    -- oldest one, which will be at the first index.
    if #recent_clicks > 2 then
        table.remove(recent_clicks, 1)
    end

    -- If we have two clicks then find which positions in
    -- tower_image_map they fall within and switch those images.  Then
    -- remove both clicks so that the player can click twice more to
    -- switch two more images (unless you don't want this behavior).
    if #recent_clicks == 2 then
        local t1 = find_tower_image_at(recent_clicks[1].x, recent_clicks[1].y)
        local t2 = find_tower_image_at(recent_clicks[2].x, recent_clicks[2].y)

        -- One or both of the clicks may not be on any tower.
        if tower_one and tower_two then
            local temp_image = tower_image_map[t1].image
            tower_image_map[t1].image = tower_image_map[t2].image
            tower_image_map[t2].image = temp_image
            recent_clicks = {}
        end
    end
end
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Re: Click on a image, do complicated stuff.

Post by Raylin »

So, I didn't see your reply until now. And I'm still fiddling with my code.
What I have now HALF-works. If you click off-tile, it will show.
It looks like it's on the wrong axis now...

Comments?

EDIT: FIXED IT! The only thing left to do is the swapping logic! :D
EDIT 2: Latest version uploaded. Having issues with the swapping part. Trying to implement the other guy's work now...
EDIT 3: I hacked up something terrible and I need help with the swapping. I'm not getting anywhere. If possible, can someone help me make it with the current system I have?
Attachments
HoC v3_2.love
DONE GOOFED
(40.23 KiB) Downloaded 125 times
HoC v3.love
(40.26 KiB) Downloaded 113 times
HoC v2.love
Still a hot mess.
(40.1 KiB) Downloaded 118 times
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Click on a image, do complicated stuff.

Post by ejmr »

I ran into an issue with this when trying to test your latest code:

Code: Select all

function love.draw()
	g.setColor(255,0,0)
	g.rectangle("fill",0,winY-50,winX,50)
	g.setColor(0,0,205,110)
	g.draw(tower)
	g.draw(player,((card.w+10)*(1-1)+150),((card.h+10)*(5-1)+50))
	if currentCard.value ~= 0 then
		g.rectangle("fill",currentCard.x,currentCard.y,card.w,card.h)
	end
	g.print(currentCard.value,0,0)
end
The call to g.print() crashes because currentCard is nil and the function expects a string. Dumping its value (and the value of other tables) at certain points can help track down problems. Personally I like to use table.show() for this purpose, which is a utility function you can find at the bottom of this page:

http://lua-users.org/wiki/TableSerialization
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests