Share Your Code

Ouya Support

Posted by RedPistonEB, Posted on January 7, 2013

GitHub URL: 
https://github.com/redpiston/CoronaOuyaController

Easily add OUYA controller support for your Corona SDK games. Check out
main.lua to see an example.

Check out the video of our game Zombie Dash which uses this class: http://youtu.be/zq8mSTrpmqA. Or download the app for iOS on itunes: https://itunes.apple.com/us/app/the-zombie-dash/id584241591

main.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
local function ouyaListener( event )
 
        --add your button specific code here
 
        --EXAMPLE:
        --if event.buttonName == "o" and event.phase == "pressed" then
        --      player.jump()
        --elseif event.buttonName == "u" and event.phase == "pressed" then
        --      player.punch()
        --end
        
        --Usable buttons as of build #971
        --O, U, A, D-Pad, Left Thumbstick
end
 
local function main()
        --set the event listener for the controller
        ouya:setListener( ouyaListener )
        
        --use this to remove the event listener when not waiting on controller input
        --ouya:removeListener( )
end

OuyaController.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module(..., package.seeall)
 
local ouya = display.newGroup()
 
local function ouyaListener( event )
        
        local e = {}
 
        if (event.keyName == "center") then
                e.buttonName = "o"
        elseif (event.keyName == "menu") then
                e.buttonName = "u"
        elseif (event.keyName == "back") then
                e.buttonName = "a"
        else
                e.buttonName = event.keyName
        end
 
        if (event.phase == "down") then
                e.phase = "pressed"
        elseif (event.phase == "up") then
                e.phase = "released"
        end
 
        ouya.callback( e )
        
        return true
end
 
function ouya:removeListener( ) 
        Runtime:removeEventListener( "key", ouyaListener )
end
 
function ouya:setListener( callback )
        
        ouya.callback = callback
                
        Runtime:addEventListener( "key", ouyaListener )
end
 
return ouya


Replies

spideri
User offline. Last seen 11 hours 29 min ago. Offline
Joined: 26 Jan 2011

Wow, great work! I tried to like it but I get a website error.

AlanPlantPot
User offline. Last seen 2 weeks 2 days ago. Offline
Joined: 16 Aug 2011

How do we access the d-pad and thumbsticks?
It looks like you've remapped the Android buttons to the O, U and A buttons, but I don't see how you are accessing the d-pad and sticks.

RedPistonEB
User offline. Last seen 4 days 11 hours ago. Offline
Joined: 1 Nov 2011

 e.buttonName = event.keyName

This part of the code is just passing the left, right, up and down. It doesn't detect the D-Pad and the left thumbsitck as different buttons, instead both will return with "up", "down", "left", "right". Hopefully that clears everything up. Unfortunately the right thumbstick does not return anything...yet. Hopefully the Corona staff will be able to do more with future builds.

CineTek
User offline. Last seen 18 weeks 5 days ago. Offline
Joined: 29 Mar 2012

Hey, thank you for sharing this code snippet :)

One question because I do not own one of these consoles yet:

How does the image scaling work? Did you have to change a few things in config.lua or anywhere else in order to get it work in such a good way?

RedPistonEB
User offline. Last seen 4 days 11 hours ago. Offline
Joined: 1 Nov 2011

I didn't have to change my config.lua at all. It was already designed to handle multiple resolutions (for all the different android screens). However I am using the "zoomstretch" scaling so it might not be a solution for everyone.

Here's my config.lua:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if ( display.pixelHeight / display.pixelWidth > 1.72 ) then
        application =
        {
                content =
                {
                        width = 320,
                        height = 570,
                        scale = "zoomstretch",
                        fps = 30,
                },
        }
else 
        application =
        {
                content =
                {
                        width = 320,
                        height = 512,
                        scale = "zoomstretch",
                        fps = 30,
                },
}
end

As for retina display graphics or using Corona's dynamic scaling, I am unsure since I use my own way to handle things like that. The one game I used to test with, 99% of the code went unchanged besides handling the controller.

Hopefully this answers your question.

CineTek
User offline. Last seen 18 weeks 5 days ago. Offline
Joined: 29 Mar 2012

Yeah thank you very much :) I will test my game on the android simulator with the settings provided in the OUYA developer program. Then I can tweak things a little bit and see if it works.

thedavebaxter
User offline. Last seen 1 week 6 days ago. Offline
Joined: 12 Jan 2012

Unless Corona implement IAP for Ouya, I don't think we can develop games/apps for it yet.

I believe ALL games/apps have to offer a free trial and use IAP to purchase the full version.

This is something Corona should look at, Unity has support and others will have it soon.

Dave

Al Sutton
User offline. Last seen 1 year 7 weeks ago. Offline
Joined: 5 Feb 2013

I'm not too familiar with Corona, but, I'm pretty familiar with the OUYA Development Kit having been involved in it's development :).

Some of the buttons have fallbacks, which it seems like you're picking up, so I've posted the correct mappings (as used by the ODK) in our forums at http://forums.ouya.tv/discussion/665/android-button-mappings

Hope it's useful.

Al.