I blueprint an opensource game project.And when I copy and paste this class into my project
import com.badlogic.gdx.controllers.Controllers;does not exist. I think this code is very old. And because of this getControllers() method does not work.
How can I update this code. I put this class below.
package com.good.game.controls;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.controllers.Controllers; // this import does not exist
public class InputController {
public static final InputController instance = new InputController();
public InputController () {
Gdx.input.setCatchBackKey(true);
}
public boolean controllerEnabled() {
return (Controllers.getControllers().size > 0); //Cannot resolve Controllers
}
public boolean axisLeft() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getAxis(8) == -1 //Cannot resolve Controllers
|| Gdx.input.isKeyPressed(Keys.LEFT));
}
public boolean axisRight() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getAxis(8) == 1//Cannot resolve Controllers
|| Gdx.input.isKeyPressed(Keys.RIGHT));
}
public boolean axisUp() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getAxis(9) == -1)//Cannot resolve Controllers
|| Gdx.input.isKeyPressed(Keys.UP);
}
public boolean axisDown() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getAxis(9) == 1)//Cannot resolve Controllers
|| Gdx.input.isKeyPressed(Keys.DOWN);
}
public boolean buttonA() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getButton(96) || //Cannot resolve Controllers
Gdx.input.isKeyPressed(Keys.Z));
}
public boolean buttonB() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getButton(97)||//Cannot resolve Controllers
Gdx.input.isKeyPressed(Keys.C));
}
public boolean buttonX() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getButton(99) ||//Cannot resolve Controllers
Gdx.input.isKeyPressed(Keys.X));
}
public boolean buttonY() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getButton(100));//Cannot resolve Controllers
}
public boolean rightTrigger() {
return (controllerEnabled() && Controllers
.getControllers().get(0).getAxis(7) == 1);//Cannot resolve Controllers
}
public boolean closeButton() {
return Gdx.input.isKeyPressed(Keys.BACK) ||
Gdx.input.isKeyJustPressed(Keys.ESCAPE);
}
}
You have to add the gdx-controllers extension to your dependencies. It was extracted from the main project.
Related
I don`t understand how savind data from model (like User.php)
When i run saveData() from Controller new row not creared.
class Users extends \yii\db\ActiveRecord
{
....
public function saveData() {
$this->name = 'test_user';
$this->save();
}
}
I don`t want save data from controller.
What i do wrong?
Check if there are validation errors:
class Users extends \yii\db\ActiveRecord
{
....
public function saveData() {
$this->name = 'test_user';
if($this->save() == false)
{
var_dump($this->errors);
}
}
}
I want to create keyboard shortcuts in my application, for instance Ctrl+C for copy. Also I want to ignore that shortcut if a TextBox has focus.
First you need a way to check if control key is pressed. CoreWindow.GetKeyState will do the job but might be a little bit tricky to use so I created an helper class:
public class ModifierKeys
{
#region ShiftIsDown property
public static bool ShiftIsDown
{
get
{
return (Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift) & CoreVirtualKeyStates.Down) != 0;
}
}
public static bool OnlyShiftIsDown
{
get
{
return ShiftIsDown && !AltIsDown && !ControlIsDown;
}
}
#endregion
#region AltIsDown property
public static bool AltIsDown
{
get
{
return (Window.Current.CoreWindow.GetKeyState(VirtualKey.Menu) & CoreVirtualKeyStates.Down) != 0;
}
}
public static bool OnlyAltIsDown
{
get
{
return !ShiftIsDown && AltIsDown && !ControlIsDown;
}
}
#endregion
#region ControlIsDown property
public static bool ControlIsDown
{
get
{
return (Window.Current.CoreWindow.GetKeyState(VirtualKey.Control) & CoreVirtualKeyStates.Down) != 0;
}
}
public static bool OnlyControlIsDown
{
get
{
return !ShiftIsDown && !AltIsDown && ControlIsDown;
}
}
#endregion
#region NoModifierKeyIsDown property
public static bool NoModifierKeyIsDown
{
get
{
return !ShiftIsDown && !AltIsDown && !ControlIsDown;
}
}
#endregion
}
Now in OnNavigateTo/From in your page subscribe/unsubscribe on key down events:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
/*...*/
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
/*...*/
Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
}
The CoreWindow_KeyDown will then looks something like this:
void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
var focusedElement = FocusManager.GetFocusedElement();
if (args.KeyStatus.WasKeyDown == false && ModifierKeys.OnlyControlIsDown &&
!(focusedElement is TextBox)
)
{
if (args.VirtualKey == VirtualKey.X)
{
/*...cut...*/
}
else if (args.VirtualKey == VirtualKey.V)
{
/*...paste...*/
}
else if (args.VirtualKey == VirtualKey.C)
{
/*...copy...*/
}
}
}
When using the following code
public static boolean isDown;
public boolean keyDown(int key) {
if (key == Keys.SPACE) {
isDown = true;
KeyMethods.testKeyDown();
}
if (key == Keys.DOWN) {
KeyMethods.testKeyPressed();
}
return false;
}
public boolean keyUp(int key) {
if (key == Keys.SPACE) {
isDown = false;
}
return false;
}
The keyUp is not setting the isDow to false. The testKeyDown:
public static void testKeyDown() {
while (GameKeyListener.isDown) {
System.out.println("Down");
}
}
It just creates an infinite loop. Is there any other way to do this?
You have to return true at the very end of the keyDown/keyUp methods. this indicates that the event is handled.
I'm new to Action-script OOP and i need to know how to chain methods like this example i have
I.$(button).bind('click',clickButton).bind('rollover',overButton).bind('rollout',outButton)
First i need to remove the I. to use dollar sign only like jQuery :) to select MovieClip and apply any action on it second issue that i have because this way i'm using static Methods Action-script restrict's me to use only static property saving the last one who called the action here is the class code to know what i mean:
package com.MAIN
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class I extends Sprite
{
private static var cSelector:Sprite;
public static function $(selector:Sprite)
{
cSelector = selector
return I;
}
public static function alpha(val:Number)
{
cSelector.alpha = val;
return I;
}
// bind mouse event to the element
public static function bind(EventStr,func:Function)
{
var func1:Function = function(e:MouseEvent){
func(cSelector);
}
// select the event from the list
if(typeof(EventStr) == 'string'){
// map the events in lowercase
var events:Object = {click:'CLICK',rollover:'ROLL_OVER',rollout:'ROLL_OUT',dblclick:'DOUBLE_CLICK',mousedown:'MOUSE_DOWN',mousemove:'MOUSE_MOVE',mouseout:'MOUSE_OUT',mouseover:'MOUSE_OVER',mouseup:'MOUSE_UP',mousewheel:'MOUSE_WHEEL'};
// check if the event exists in the list
if(events[EventStr] && MouseEvent[events[EventStr]]){
cSelector.addEventListener(MouseEvent[events[EventStr]],func1);
}
}else if(typeof(EventStr) == 'object'){
// add the event
cSelector.addEventListener(EventStr,func1);
}
return I;
}
public static function remove()
{
cSelector.parent.removeChild(cSelector);
return I;
}
}
}
Here you go, some steps in the right direction. However, this is a really, really, really crappy idea.
//$.as
package
{
import flash.display.DisplayObject;
//NOTE: there's NO class definition
public function $( selector : DisplayObject ) : IDisplayObject
{
//traverse displaylist to find <code>selector</code>
//and return an instance of IDisplayObject that holds the reference
}
}
//IDisplayObject.as
package
{
public interface IDisplayObject{
function alpha( value : Number ) : IBinding;
}
}
//IBinding.as
package jquery
{
public interface IBinding{
function bind( eventName : String, callback : Function, ...parameters ):void;
}
}
Once you've created concrete implementations of these you can do:
$( someMC ).alpha( .5 ).bind( 'click', function(){ trace( 'what a miraculously crappy idea !!!!' ) } );
You could try it like this:
interface Test {
function doBla(): Test
function moreBla(): Test
}
public class StaticTest {
private static const instance: Test = new InternalTest()
public static doBla() : Test {
return instance.doBla();
}
public static moreBla() : Test {
return instance.moreBla();
}
}
internal class InternalTest implements Test {
function doBla(): Test {
trace("bla");
return this;
}
function moreBla(): Test {
trace("more bla");
return this;
}
}
i just know this is a dumb question, so excuse me in advance.
i want to essentially classify a simple function in it's own .as file. the function compares integers. but i don't know how to call the class and receive a boolean return.
here's my class
package
{
public class CompareInts
{
public function CompareInts(small:int, big:int)
{
compare(small, big);
}
private function compare(small:int, big:int):Boolean
{
if (small < big)
return true;
else
return false;
}
}
}
so now i'd like to write something like this:
if (CompareInts(1, 5) == true). or output 'true' by writing trace(CompareInts(1, 5));
You have 2 options:
1) Make your compare function public and static.
static public function compare(small:int,big:int):Boolean {
{
if (small < big)
return true;
else
return false;
}
}
And call it:
CompareInts.compare(1,5);
2) You don't actually need a class. You can just use a function. As long as there's only one public definition per AS file, you'll be fine (by that I mean that at the "top" level you can have a public class, an interface, a public function, a public var or a public namespace. It won't work if you try to have more than one.
package {
public function compare(small:int,big:int):Boolean {
{
if (small < big)
return true;
else
return false;
}
}
Or, you could inline this into a single line and ditch the class / function entirely (parens are not neccesary, I just added them for clarity):
var compare:Boolean = (small < big);
You are creating a Class and then function that are members of that Class so for calling them you have to instanciate the Class and then call the function of the instance created.
Also CompareInts is the constructor of the class it can't return anything
the working code corresponding to your class will be:
package {
public class CompareInts {
public function CompareInts(){}
public function compare(small:int, big:int):Boolean {
if (small < big)
return true;
else
return false;
}
}
}
new CompareInts().compare(1, 2);
But this a litle bit overkill so what you can do is create a static function into your class and then call it directly without the needed to instanciate the class.
package {
public class Compare {
public static function compareInts(small:int, big:int):Boolean {
if (small < big)
return true;
else
return false;
}
}
}
Compare.compareInts(1, 2)
If you don't want to group more functions into a Class you can always declare a single function into a Package:
package {
public function compareInts(small:int, big:int):Boolean {
if (small < big)
return true;
else
return false;
}
}
An AS3 constructor can never have a return value. If you want to keep this method in a class, then have the constructor just call an init() function and have init() return values.
Hope that helps clear up the "why?" that might be swirling in your head.