Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Simple game project to understand keyeventlisteners
Started by henriquesv Sep 17 2017 11:31 AM

1 reply to this topic
remote control tvos testing
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

henriquesv

[GLOBAL: userInfoPane.html]
henriquesv
  • Observer

  • 10 posts
  • Corona SDK

Hello guys! I've been trying to understand pew pew sample project but I found it a bit complex.

 

Could you please help me out here?

 

What is the real listener? The one below?

Runtime:addEventListener( "key", onKeyEvent )

 

I do have to activate it first?

system.activate("controllerUserInteraction")

 

I have also tried to maintain a .lua controller but I can't see anything at log screen:

 

local composer = require( "composer" )

local controller = {}
 
-- Get Input Device Descriptor
local inputDevices = system.getInputDevices();
 
for i = 1,#inputDevices do
    local device = inputDevices[i];
    print(device.displayName);
end
 
 
-- Check the Status of the Input Device
local function onInputDeviceStatusChanged( event )
 
    if ( event.device.isConnected ) then
    --if ( event.connectionStateChanged ) then
        if ( event.device.isConnected ) then
            -- Device has been connected
            print(event.connectionStateChanged);
            print(event.device);
            print(event.name);
            print(event.reconfigured);
            print("Device is connected");
        else
            -- Connection has been lost
            print("Device not connected");
        end
    end
end
 
Runtime:addEventListener( "inputDeviceStatus", onInputDeviceStatusChanged );
 
----------------------------------------------------------------------
-- BEGIN COMPOSER SETVARIABLE EXAMPLE
----------------------------------------------------------------------
-- Handle all Key Events
function onKeyEvent( event )
    setDevice( event.device, event.device.displayName );
   
 

    if (event.phase == "down") then
        keyPos = event.keyName;
        print(keyPos);

        -- assign the keyPos to the Runtime:dispatchEvent Object
        Runtime:dispatchEvent( {
            name = "controllerKeyPos",
            key = keyPos

        } );
    end

    return keyPos;
end
 
composer.setVariable( "function_onKeyEvent", onKeyEvent )
 
Runtime:addEventListener( "key", onKeyEvent );
 
return controller;
 
----------------------------------------------------------------------
-- END COMPOSER SETVARIABLE EXAMPLE
----------------------------------------------------------------------

 

 

I am using a Nimbus controller for testing with my Mac. Whenever I connect it I receive a few lines at debugging screen:

 

Sep 17 04:21:57.403 true
                    userdata: 0x60000025d878
                    inputDeviceStatus
                    false
                    Device is connected
Sep 17 04:21:57.423 false
                    userdata: 0x60000025d878
                    inputDeviceStatus
                    true
                    Device is connected
                    true
                    userdata: 0x60000025d878
                    inputDeviceStatus
                    false
                    Device is connected

 

 

Thanks!



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,263 posts
  • Enterprise

Before I jump into this, please use code formatting when including code.  You can click on the blue <> button in the row with Bold and Italic buttons and then paste your code into the popup box.

 

Pew Pew covers a lot of use cases that you will eventually want to implement.  Controller detection is very useful because batteries die, people use multiple controllers, and you want to handle all of those circumstances. 

 

But the simple use case is to write a function to handle the various controller button presses and then once you have the function written, then you can call 

 

Runtime:addEventListener( "key", onKeyEvent )

 

(assuming "onKeyEvent" is the name of your function) to enable it.  You can do this in main.lua for simple usage. Your function would look for key presses and react to the various events generated.

 

You can also look at this sample:  https://github.com/coronalabs/steamworks-sample

 

For the controller code there.  The various Ponywolf templates in the Marketplace also have a simple controller interface I believe.

 

Now this starts ramping up in complexity fairly quickly in a several areas.

 

1. Controllers have different key names.  A Playstation Controller might generate a "Button12" for an up button, where an X-Box controller will generate "up". You will need a way to map your controller buttons to something  easier to program against. Even the buttons between a PS3 and PS4 controller will generate different values.  PewPew uses a system that lets the user decide what button they want for up, down, left, right etc. Many games to this route. 

 

2. Some controllers will need axis support to get  your up/down/left/right movement. That's a whole different event handler.

 

3. If you have multiple composer scenes, then you may want a unique controller handler per scene. In the game I'm making in my menu scene, I only care about up, down and ButtonA events. I move a small graphic with an A in the middle to the selected menu button and on that press I call the tap handler for the menu item. But when I'm in the game scene I use more buttons to do things, so it has it's own onKeyEvent function. I have to disable the menu one before I enable the game one and so on.

 

Controllers are fun to play with but can be a little bit of work to get going.  I would suggest you start simple and build up from there.

 

Rob




[topic_controls]
[/topic_controls]