Get Siri Remote orientation (or get notifications of change?) - tvos

I'm been searching for a way to check the Siri Remote's current orientation or to register for Siri Remote orientation changes, but I haven't found anything yet. Is it possible to do this (without resorting to interpreting the raw gravity data)?
I've have found out how to disable auto-orientation changes with "allowsRotation" on "microGamepad". Which is pretty cool!

I didn't see an API either, however as you mentioned, you can poll for the gravity data, and I just wanted to post that code here in case some find it useful. You can modify this to your own need, such as detecting last orientation and comparing it to current if you want a callback of the change.
//**************************************************************
// Update loop portion to UIViewController
//**************************************************************
#property (strong) CADisplayLink *updateLoopTimer;
self.updateLoopTimer = [CADisplayLink displayLinkWithTarget:self selector:#selector(updateRefreshRate:)];
[self.updateLoopTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-(void)updateRefreshRate:(CADisplayLink *)displayLink
{
CFTimeInterval deltaTime = displayLink.duration * displayLink.frameInterval;
[self update:(float)deltaTime];
}
//******************************************************
// Update loop
//******************************************************
-(void)update:(float)dt
{
#ifdef TV
//***************************************
// Detect button presses
//***************************************
//self.gameControler = [[GCController controllers] firstObject];
if( self.gameController != nil )
{
GCMicroGamepad* microPad = self.gameController.microGamepad;
if ( microPad != nil )
{
GCMotion *motion = self.gameController.motion;
GCControllerDirectionPad *dpad = microPad.dpad;
if( motion != nil )
{
GCAcceleration accelVector = motion.gravity;
if( fabs(accelVector.x) > fabs(accelVector.y) )
{
NSLog(#"Sideways");
}
else
{
NSLog(#"Upright");
}
}
}
}
#endif
}

Related

kotlin try catch block with bundle.getString

We have intent extras that are passed back to an Activity
The code is written with Kotlin 1.3 and is posted below
We do not understand why the code needs to be in a try catch block
Our question is there a better way to write this code and can someone explain why the code requires a try catch block. We know it could be written with when.
The navigation back to this Activity is accomplished with various intents that do not always put all the values that the bundle gets.
One button uses this code
val intent = Intent(this,MainActivity::class.java)
intent.putExtra("FROM", "NEW")
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
startActivity(intent)
While another button uses this code
holder.ivEdit.setOnClickListener {
//val rowid = friendList.get(position).id
val intent = Intent(context, MainActivity::class.java)
intent.putExtra("FROM", "UPDATE")
intent.putExtra("recordID", items.id)
intent.putExtra("PERSON", items.person)
intent.putExtra("PHONE", items.phone)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
Here is the code that is in the Activity that has the try catch code
The code is inside the onCreate function
try {
val bundle: Bundle = intent.extras
from = bundle.getString("FROM","")
txtPerson = bundle.getString("PERSON","")
txtPhone = bundle.getString("PHONE","")
if(from == "UPDATE") {
showMSG("To CANCEL use back button")
id = bundle.getInt("recordID", 4)
btnAdd.visibility = View.INVISIBLE
btnEdit.visibility = View.VISIBLE
btnViewList.visibility = View.INVISIBLE
etPerson.setText(txtPerson)
etPhone.setText(txtPhone)
}else if (from == "DELETE"){
showMSG("To CANCEL use back button")
btnAdd.visibility = View.INVISIBLE
btnViewList.visibility = View.INVISIBLE
btnEdit.visibility = View.INVISIBLE
btnDelete.visibility = View.VISIBLE
etPerson.setText(txtPerson)
etPhone.setText(txtPhone)
etPerson.isEnabled = false
etPhone.isEnabled = false
}else{
btnViewList.visibility = View.VISIBLE
btnAdd.visibility = View.VISIBLE
btnEdit.visibility = View.INVISIBLE
}
if (id != 0) {
//etPerson.setText(txtPerson)
//etPhone.setText(txtPhone)
}
} catch (ex: Exception) {
}
The guess here is that the Activity with the try catch is also navigated to by another activity that passes no information for the bundle so the bundle gets set to null
intent.extras must not be null so if it is null you need a way to deal with that fact
I do not see a better way around the issue than the try catch block
perhaps someone can offer another solution.

Extra "Space" at end of "If" Requirement AS3

My code works, except it is requiring and extra "Space" at the end to be placed in order for the button to activate? Any ideas? I obviously don't have the space at the end of the user names or passwords in the code. This happens on another frame as well where I have the user type in a web address, I have the conditional set as == "md.website.com" but it is requiring "md.website.com " (extra space at the end) in order for the button to activate.
This code is expecting "AB1234 " and "newuser " instead of "AB1234" "newuser" like I need and I am telling it... I'm sorry, I'm new to AS3 and learning ALL I can, this site rocks for all the help I've already gotten!
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text == "AB1234" && password_txt.text == "newuser" )
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}
The problem is that the TextEvent.TEXT_INPUT fires before the text field is actually updated. Try using Event.CHANGE instead (or using two TextEvent.TEXT_INPUT callbacks and appending the input character with event.text within each).
I don't know why AS3 is requiring the extra space, but I removed the exact conditional, and just did a minimum character count. Of course the trainee can then type in anything as long as it matches the minimum requirement, but again, the actual usernames and passwords don't matter, its all simulation anyways, here is the code with the character count....
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text != "" && username_txt.length >=5 &&
password_txt.text != "" && password_txt.length >=6)
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}

Libgdx preferences not working on mobile

When i have tested my game on desktop the prefs work perfectly and correct scores and highscore is output but when i run it on mobile the values do not save and for both scores show 0. There is no errors also. Just need some help to try and fix this. Thanks alex.
pref = Gdx.app.getPreferences("Scores");
if(pref.contains("Score") == false) {
pref.putInteger("Score", 0);
if(pref.contains("HighScore") == false){
pref.putInteger("HighScore", 0);
}
}
GameOver Screen
pref = Gdx.app.getPreferences("Scores");
score = pref.getInteger("Score");
highScore = pref.getInteger("HighScore");
if(score > highScore){
pref.putInteger("HighScore", score);
highScore = score;
}
This is ran when the player dies.
public void updateScore(){
if (pref.contains("Score") == true){
}
pref.putInteger("Score", hud.getScore());
}
You need to call flush() method after putting a value to preferences - it causes commiting data to the memory
pref.putInteger("Score", hud.getScore()).flush();
The a look at official wiki also

Save Number with Shared Object and Add to that Saved Number.

Hey everyone so basically what I am trying to accomplish is saving a number with the Shared Object which the the coins that the player collects in the game and if they player quits out of the game and comes back to play again the amount of coins he had when he quit will still show and he will be able to add to that amount if the player picks up more coins. I had this working for a bit but then the textfield started displaying "NAN".
Here is how I have it set up.
The Variable private var nCoins:Number;
In the constructor function I have:
sharedObjectCoins = SharedObject.getLocal("CoinsData");
nCoins = 0 + sharedObjectCoins.data.tCoins;
if (sharedObjectCoins.data.tCoins == null)
{
sharedObjectCoins.data.tCoins = nCoins;
}else
{
trace("Save data found."); // if we did find data...
loadDataTimeAttack(); // ...load the data
}
and in the games Enter.Frame Loop I have the function saveDataCoins which is setup like so:
private function saveDataCoins():void
{
if (nCoins > sharedObjectCoins.data.tCoins )
{
sharedObjectCoins.data.tCoins = nCoins;
}
coinsGraphic.coinsText.text = " " + sharedObjectCoins.data.tCoins;
sharedObjectCoins.flush();
}
not sure if you need the function to where the hitTest takes place between the coins and player but here it is:
private function checkPlayerHitCoins():void
{
for (var i:int = 0; i < aCoinsArray.length; i++)
{
//get current point in i loop
var currentCoins:mcCoin = aCoinsArray[i];
//test if player is hitting current point
if(player.hitTestObject(currentCoins))
{
nCoins += 1;
updatecoinsTextScore();
updateCoinsPauseScreen();
//Add points sound effects
var coinsSEffect:Sound = new coinsSound();
coinsSEffect.play();
//remove point on stage
currentCoins.destroyCoins();
//remove points from array
aCoinsArray.splice(i, 1);
trace("Hit: " + aCoinsArray.length);
}
}
}
Please if anyone could help me with this maybe point something out that I am doing wrong. This code worked perfect one time and when I closed the screen and came back to re test it the textfield displayed NAN and thats it when I hitTest the coins sometimes the NAN switches to a number for like a second but then goes back to NAN.
The first time (or rather every time it creates a new shared object) you will be trying to add undefined to 0, which will result in either a runtime error or NaN.
You need to check if the value exists before attempting to do addition with it.
if(sharedObjectCoints.data && sharedObjectCoins.data.tCoins && !isNaN(sharedObjectCoins.data.tCoins)){
nCoins = Number(sharedObjectCoins.data.tCoins); //there's not point in adding 0
trace("Save data found."); // if we did find data...
loadDataTimeAttack(); // ...load the data
}else{
sharedObjectCoins.data.tCoins = nCoins;
}
Also, if you don't manually set a value to a number var, it will start off life as NaN. eg var nCoins:Number will be NaN until you set it to something.
That said, working with the sharedObject directly like this is a very sloppy way to code your program. Really you should just use shared object to load and save the value, and everything in between use a strongly typed variable.
var nCoins:int = 0;
var tCoins:int = 0;
sharedObjectCoins = SharedObject.getLocal("CoinsData");
if(sharedObjectCoins.data && sharedObjectCoins.data.tCoins && !isNaN(sharedObjectCoins.data.tCoins){
tCoins = int(sharedObjectCoins.data.tCoins);
}else{
//no shared object, use default value for tCoins
tCoins = 0; //or whatever it should start off as.
}
Then write a save function
private function saveSharedObject():void {
sharedObjectCoins.data.tCoins = tCoins;
sharedObjectCoins.flush();
}
Then replace all other instances of sharedObjectCoins.data.tCoins with the var tCoins
It's probably best not to flush the shared object every frame for performance purposes.
Also, shared objects may or may not actually save, depending on user preferences, storage space available, etc. They should not be relied upon for critical data retention.
You can listen for problems with the shared object with AsyncErrorEvent.ASYNC_ERROR I believe (It's been a while since I've worked with AS3 Shared Objects)

Speech API (SAPI) floating point division by zero in C++ Builder on Windows 7

I use the following code for Text-To-Speech application controls for blind persons in C++ Builder (most likely similar example can be used in Delphi). Main form has KeyPreview property checked to enable key F11 preview to start speaking active (focused) control. The code as it is works but there are some problems. This example is in C++ Builder code but from what I've found, Delphi suffers from same problem and the solution I found is the same. If you have Delphi solution, feel free to post it, it is similar anyway.
#include <sapi.h>
#include <WTypes.h>
//---------------------------------------------------------------------------
// Speak text string (synchronous function)
//---------------------------------------------------------------------------
bool SpeakText(UnicodeString Text)
{
ISpVoice* pVoice = NULL;
if (FAILED(::CoInitialize(NULL))) return false;
Word Saved8087CW = Default8087CW; // Disable floating point division by zero exception caused by Speak
Set8087CW(0x133f);
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if (SUCCEEDED(hr))
{
//pVoice->SpeakCompleteEvent()
//pVoice->SetSyncSpeakTimeout(1000);
hr = pVoice->Speak(WideString(Text).c_bstr(), SPF_DEFAULT, NULL);
pVoice->Release();
pVoice = NULL;
}
Set8087CW(Saved8087CW);
::CoUninitialize();
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
UnicodeString Speaker;
if (Key == VK_F11)
{
if (Screen->ActiveControl->InheritsFrom(__classid(TButton))) { Speaker += "Button, " + static_cast<TButton*>(Screen->ActiveControl)->Caption + "."; }
else if (Screen->ActiveControl->InheritsFrom(__classid(TEdit))) { Speaker += "Edit box, " + static_cast<TEdit*>(Screen->ActiveControl)->Text + "."; }
}
if (Speaker != "") SpeakText(Speaker);
}
//---------------------------------------------------------------------------
Problems:
pVoice->Speak causes Floating point division by zero if I don't override the exception using the Set8087CW function. This happens only on Windows 7 (possibly Vista and Windows 8 too) but not on Windows XP in the same program (compiled exe). Is there a solution without using Set8087CW? Removing these lines will cause the problem and exception. I have BCB2010.
Function is synchronous and won't shut up or return control to program until it finishes reading text. This is a problem for longer text. It also blocks program events. Is there a way to make it asynchronous or introduce an event to periodically check for F11 key status and if F11 is pressed again it stops reading and uninitializes object? For example poll every 300 ms (or after each word etc.) for key-press F11 and if pressed, stop speaking? Or run it threaded?
Does SAPI has memory leaks as some write on various sites?
Can above code use OleCheck instead of CoCreateInstance and CoUninitialize?
UPDATE for those looking for solution as suggested by Remy Lebeau:
SavedCW = Get8087CW();
Set8087CW(SavedCW | 0x4);
hr = pVoice->Speak(WideString(Text).c_bstr(), SPF_DEFAULT | SPF_ASYNC, NULL);
pVoice->WaitUntilDone(-1); // Waits until text is done... if F11 is pressed simply go out of scope and speech will stop
Set8087CW(SavedCW);
Also found detailed example in CodeRage 4 session: http://cc.embarcadero.com/item/27264
The error does occur in Vista as well. Masking floating point exceptions is the only solution.
To make Speak() run asynchronously, you need to include the SPF_ASYNC flag when calling it. If you need to detect when asynchronous speaking is finished, you can use ISpVoice::WaitUntilDone(), or call ISpVoice::SpeakCompleteEvent() and pass the returned HANDLE to one of the WaitFor...() family of functions, like WaitForSingleObject().
What kind of leaks do other sites talk about?
Not instead of, no. OleCheck() merely checks the value of an HRESULT value and throws an exception if it is an error value. You still have to call COM functions that return the actual HRESULT values in the first place. If anything, OleCheck() would be a replacement for SUCCEEDED() instead.
For what you are attempting, I would suggest the following approach instead:
struct s8087CW
{
Word Saved8087CW;
s8087CW(Word NewCW)
{
Saved8087CW = Default8087CW;
Set8087CW(NewCW);
// alternatively, the VCL documentation says to use SetExceptionMask() instead of Set8087CW() directly...
}
~s8087CW()
{
Set8087CW(Saved8087CW);
}
};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
::CoInitialize(NULL);
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
if (pVoice) pVoice->Release();
::CoUninitialize();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
if (Key == VK_F11)
{
TWinControl *Ctrl = Screen->ActiveControl;
if (Ctrl)
{
TButton *btn;
TEdit *edit;
if ((btn = dynamic_cast<TButton*>(Ctrl)) != NULL)
SpeakText("Button, " + btn->Caption);
else if ((edit = dynamic_cast<TEdit*>(Ctrl)) != NULL)
SpeakText("Edit box, " + edit->Text);
}
}
}
//---------------------------------------------------------------------------
ISpVoice* pVoice = NULL;
bool __fastcall TForm1::SpeakText(const String &Text)
{
s8087CW cw(0x133f);
if (!pVoice)
{
if (FAILED(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice)))
return false;
}
SPVOICESTATUS stat;
pVoice->GetStatus(&stat, NULL);
while (stat.dwRunningState == SPRS_IS_SPEAKING)
{
ULONG skipped;
pVoice->Skip(L"SENTENCE", 1000, &skipped);
pVoice->GetStatus(&stat, NULL);
}
return SUCCEEDED(pVoice->Speak(WideString(Text).c_bstr(), SPF_ASYNC, NULL));
}