Conditional Statements (please see explanation) - android

Ok, I have three conditions here. It's seems pretty simple, but I can't get it working how I want, bear with me as I try to explain it.
If(conditionA){
Play Sound 1}
If(conditionB) {
changedrawable ///(irrelevant)
}
If(conditionA && conditionB){
Play sound 2
}
////Unfortunately, since condition A is true, it plays sound 1 too, and I only want it to play sound 2
FWIW These conditions are just coordinates on the touchscreen, and I have them set up as boolean's
Boolean conditionA;
conditionA = y > 0 && y < (0.2 * tY) && x > 0 && x < (0.1667 * tX) || y2 > 0
&& y2 < (0.2 * tY) && x2 > 0 && x2 < (0.1667 * tX);
ConditionA and ConditionB are referring to different points on the touchscreen. The main question: how on earth can I get it so when conditionA and conditionB are BOTH true, ONLY Play Sound 2 (instead of both sounds)? I have been pulling my hair out the last day trying to figure this out. Please let me know if there is a solution or if you need further details. Thanks!

This may not be perfectly optimal, but it's easy to read:
if (conditionA && conditionB) {
play sound 2
} else if (conditionA) {
// Implies that conditionB == false
play sound 1
} else if (conditionB) {
change drawable
}

Shouldn't it be
if(conditionA && conditionB) {
play sound 2
} else if(conditionA) {
play sound 1
}
if(conditionB) {
change drawable
}
You only want the else if for playing the sound. Not for changing the drawable.

How about:
if (conditionA && conditionB) {
// play sound2
} else if (conditionA) {
// play sound1
} else if (conditionB) {
// change drawable
}
If both conditions aren't satisfied, it'll run the code for the condition that is. If they are both satisfied, it'll only run //play sound2.

Just put the third option as the first, and then use else ifs in case not both conditions are fulfilled.
edit/ what he says :)

This may not be the most elegant solution, but it should work:
if (conditionA && conditionB) {
play sound 2
}
else {
if (conditionA){
play sound 1
}
if (conditionB) {
change drawable
}
}

Related

how do i make my death menu wait 3 seconds to set active after the player died?

hi everyone and thank you for your help, so i made this game where i have 2 players i can switch by touching the right half of the screen. Today i wanted to add an explosion when my player dies, but my death menu spawn as soon as the player dies so the particles are hidden by it. i tried using Waitforseconds but it doesnt work (also can you explain me why? im a rookie). Here is the code's parts i think you need to see:
this is in my OnTriggerCollision
if(collision.gameObject.tag == "spikes")
{
Break();
theGameManager.RestartGame();
moveSpeed = moveSpeedStore;
speedMilestoneCount = speedMilestoneCountStore;
speedIncreaseMilestone = speedIncreaseMilestoneStore;
}
}
private void Break()
{
if(rosso.gameObject.activeInHierarchy)
{
rb.gameObject.SetActive(false);
ParticleSystem exRossa = Instantiate(esplosioneRosso, new Vector3(transform.position.x, transform.position.y, -1), Quaternion.identity);
exRossa.Play();
}
if(verde.gameObject.activeInHierarchy)
{
rb.gameObject.SetActive(false);
ParticleSystem exVerde = Instantiate(esplosioneVerde, new Vector3(transform.position.x, transform.position.y, -1), Quaternion.identity);
exVerde.Play();
}
}
Restart game just set the death menu active and set some stuff to the original value, rosso and verde are the players, esplosione rosso and esplosione verde are the 2 types of particles.
Ty for help
You can make a coroutine Died() or Death() that wait some time before activating the death screen
IEnumerator Death(float seconds)
{
return yield new WaitForSeconds(seconds);
//Call DeathScreen.SetActive(true) after calling this function
}
Or you can use a bool in the Update function:
void Update()
{
time = 3f;
//set boolean dead to true in the event of the player dying
death = true;
if(death)
time -= 1f * Time.deltaTime;
if(death && time <= 0f)
DeathScreen.SetActive(true);
else
DeathScreen.SetActive(false);
...

Is there a way to load multiple URL's in a for loop

I'm trying to control RGB LED's with an ESP8266 module. But if i try to control multiple ESP8266 modules at once, the for loop only works for the last array element.
I've tried Toast.makeText(applicationContext, i.toString(), Toast.LENGTH_SHORT).show()
and the loop only counts 2 also if i remove the if statement.
The toast itself works fine on the AVD but not on my Galaxy S8.
The loading of all ips doesn't work on my phone and on the AVD.
Every time i try to control multiple devices only the last one lights up.
Here's my RGBActivity maybe I'm missing something.
iv_rgb.setOnTouchListener {
_,
event - >
if (event.action == MotionEvent.ACTION_DOWN || event.action == MotionEvent.ACTION_MOVE) {
bitmap = iv_rgb.drawingCache
val pixel = bitmap.getPixel(event.x.toInt(), event.y.toInt())
var r = (Color.red(pixel) * brightness) / 100
var g = (Color.green(pixel) * brightness) / 100
var b = (Color.blue(pixel) * brightness) / 100
val hex = "#" + Integer.toHexString(pixel)
for (i in Devices.deviceIP.indices) {
if (Devices.switch[i]) {
wv_rgb.loadUrl("http://${Devices.deviceIP[i]}/?r${r.toString()}g${g.toString()}b${b.toString()}&")
Toast.makeText(applicationContext, Devices.deviceIP[i], Toast.LENGTH_SHORT).show()
}
}
}
true
}
If `wv_rgb' is a WebView than for a loadUrl you have to wait for event onPageFinished() (or onPageLoaded() forgot name).
inside the loop you can try using onPageStarted() passing view url anf favicon and than call onPageFinished()(as stated by #blackapps) passing view and url. or try using conventional for loop(not sure, but worth a shot).
also you can look into this link https://android--code.blogspot.com/2016/03/android-detect-when-webview-finish.html

Get touch Location and tap on monogame

I'm writing a game in monogame for Android for my school project but I can't get touch location to do one simple tap action, I search but I can't find anything useful to do that. My game is a pipe puzzle game that I want when player touch a pipe it rotate but as I say I can't do this. this is my code for keyboard input.
KeyboardState newState = Keyboard.GetState();
if (oldState.IsKeyUp(Keys.Space) && newState.IsKeyDown(Keys.Space))
{
angle1 += (float)Math.PI / 2.0f;
}
oldState = newState;
You'll want to add a check for the following:
TouchCollection touchCollection = TouchPanel.GetState();
if (touchCollection.Count > 0)
{
//Only Fire Select Once it's been released
if (touchCollection[0].State == TouchLocationState.Moved || touchCollection[0].State == TouchLocationState.Pressed)
{
Console.WriteLine(touchCollection[0].Position);
{
}
You can then do what ever you want with the Position variable.

Issue with removing eventlistener in actionscript

I'm not new to flash, but I'm a bit of a noob with actionscript, trying to build an app in flash pro (or rather, animate cc) which will (hopefully) teach the users music theory (how to read music, etc.). What I want is to have different lessons on separate frames, with separate "screens" which the user can swipe through. I'm using multiple copies of the swipe code which adobe provides in their swipe gallery template.
On frame 5, I use the following:
stop()
Multitouch.inputMode = MultitouchInputMode.GESTURE;
var currentGalleryItem:Number = 1;
var totalGalleryItems:Number = 10;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameB);
function fl_SwipeToGoToNextPreviousFrameB(event:TransformGestureEvent):void
{
if(event.offsetX == 1)
{
if(currentGalleryItem > 1){
currentGalleryItem--;
slideRight();
}
}
else if(event.offsetX == -1)
{
if(currentGalleryItem < totalGalleryItems){
currentGalleryItem++;
slideLeft();
}
}
}
var slideCounter:Number = 0;
function slideLeft(){
lsn112.addEventListener("enterFrame", moveGalleryLeft);
}
function slideRight(){
lsn112.addEventListener("enterFrame", moveGalleryRight);
}
function moveGalleryLeft(evt:Event){
lsn112.x -= 128;
slideCounter++;
if(slideCounter == 10){
lsn112.removeEventListener("enterFrame", moveGalleryLeft);
slideCounter = 0;
}
}
function moveGalleryRight(evt:Event){
lsn112.x += 128;
slideCounter++;
if(slideCounter == 10){
lsn112.removeEventListener("enterFrame", moveGalleryRight);
slideCounter = 0;
}
}
Home112.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_22);
function fl_ClickToGoToAndStopAtFrame_22(event:MouseEvent):void
{
gotoAndStop(2);
}
stop()
Frame 6 is almost identical, just with different names for variables, functions, etc.:
stop()
Multitouch.inputMode = MultitouchInputMode.GESTURE;
var currentGalleryItemA:Number = 1;
var totalGalleryItemsA:Number = 11;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameA);
function fl_SwipeToGoToNextPreviousFrameA(event:TransformGestureEvent):void
{
if(event.offsetX == 1)
{
if(currentGalleryItemA > 1){
currentGalleryItemA--;
slideRightA();
}
}
else if(event.offsetX == -1)
{
if(currentGalleryItemA < totalGalleryItemsA){
currentGalleryItemA++;
slideLeftA();
}
}
}
var slideCounterA:Number = 0;
function slideLeftA(){
lsn113.addEventListener("enterFrame", moveGalleryLeftA);
}
function slideRightA(){
lsn113.addEventListener("enterFrame", moveGalleryRightA);
}
function moveGalleryLeftA(evt:Event){
lsn113.x -= 128;
slideCounterA++;
if(slideCounterA == 10){
lsn113.removeEventListener("enterFrame", moveGalleryLeftA);
slideCounterA = 0;
}
}
function moveGalleryRightA(evt:Event){
lsn113.x += 128;
slideCounterA++;
if(slideCounterA == 10){
lsn113.removeEventListener("enterFrame", moveGalleryRightA);
slideCounterA = 0;
}
}
Home113.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_23);
function fl_ClickToGoToAndStopAtFrame_23(event:MouseEvent):void
{
gotoAndStop(2);
}
stop()
There is also a button as part of the movieclip "lsn112" which is being swiped. Don't know if this is relevant or not, but the code is:
stop();
fwdtest.addEventListener(MouseEvent.CLICK, GoRootNext112);
function GoRootNext112(event:MouseEvent):void
{
MovieClip(root).nextFrame();
}
It works fine to a point, but I think an eventlistener is not being removed properly. When the user swipes through the gallery, it works as expected. They can then move onto the next gallery, which also works as expected. No errors so far. However, if they then go back to the menu, and then back to the gallery, I get an error code 1009:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
MusicTheorySwipe_fla::MainTimeline/slideRightA()[MusicTheorySwipe_fla.MainTimeline::frame6:32]
at
MusicTheorySwipe_fla::MainTimeline/fl_SwipeToGoToNextPreviousFrameA()[MusicTheorySwipe_fla.MainTimeline::frame6:16]
at runtime::ContentPlayer/simulationSendGestureEvent() at
runtime::SimulatedContentPlayer/clientSocketDataHandler()
What confuses me is that I am using frame 5 at this point, yet I get an error referencing frame 6. It appears to me that flash is attempting to send a gesture to the eventlistener in frame 6, even though I'm on frame 5, which I'm guessing is down to an eventlistener not being removed. However, being new to code, I don't know when to remove the eventlistener without breaking the code.
Here's a link to a zip containing the relevant .fla, .swf and .xml files.
http://speedy.sh/5JP7c/MusicTheorySwipe.zip
As this is the method I would like to use over many, many frames, I would really appreciate your time and help in resolving this.
EDIT
Ok, I've simplified the code as best I can, to try and eliminate any suspects.
Frame 5:
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeA);
var currentGalleryItemA:Number = 1;
var totalGalleryItemsA:Number = 5;
function onSwipeA (e:TransformGestureEvent):void{
//User swiped towards right
if (e.offsetX == 1) {
if(currentGalleryItemA > 1){
currentGalleryItemA--;
lsn113.x += 1280;
}
}
//User swiped towards left
if (e.offsetX == -1) {
if(currentGalleryItemA < totalGalleryItemsA){
currentGalleryItemA++;
lsn113.x -= 1280;
if(currentGalleryItemA == totalGalleryItemsA){
nextFrame()
}
}
}
}
stop();
Frame 6:
stage.removeEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeA);
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeB);
var currentGalleryItemB:Number = 1;
var totalGalleryItemsB:Number = 11;
function onSwipeB (e:TransformGestureEvent):void{
//User swiped towards right
if (e.offsetX == 1) {
if(currentGalleryItemB > 1){
currentGalleryItemB--;
lsn112.x += 1280;
}
}
//User swiped towards left
if (e.offsetX == -1) {
if(currentGalleryItemB < totalGalleryItemsB){
currentGalleryItemB++;
lsn112.x -= 1280;
}
if(currentGalleryItemB == totalGalleryItemsB){
nextFrame()
}
}
}
stop();
And that's all the actionscript there is now, yet it's still not working. Any ideas?
On frame 2 when you switch to frame 6 check if stage has the event listener fl_SwipeToGoToNextPreviousFrameA() and if so remove it. That should fix your error.
You need to remove the listener at couple of frames.
Write these lines on frame2 after all of your code
stage.removeEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameA);
stage.removeEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameB);
Write this line on frame5 before you define listener
stage.removeEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameA);
Write this line on frame6 before you define listener
stage.removeEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameB);
and remove both from any other frame you can jump from frame5 and frame6.

App works on PC but not in Android

I created an app using Unity3D. After developing it for some weeks I tried to generate an apk and test it on my SGII.
Unity returns no errors or warnings when testing the app locally, but when it runs on my phone, it doesn't work.
There are 2 buttons (1 to clock in and 1 to clock out from work). Button 1 takes the date when you clock in, and switches a boolean to allow you to clock out. When running in my phone, button 1 works fine but button 2 doesn't. The button works actually, since it returns every debug.log, but nothing else.
My code looks like this:
function OnGUI()
{
GUI.skin = skin;
if(Functions.firstTime == 0)
{
Functions.setupWiz();
}
switch(currentMenu)
{
case 1:
GUI.BeginGroup(Rect(0, 0, width, height));
if(Functions.birthday == true)
{
debugLog11 = "\n"+happyBirthdayMsg+", "+Functions.userName+"!";
Functions.birthday = false;
}
if(GUI.Button(Rect(buttonTopHMargin,height*.1f + buttonTopVMargin, width*.24f, height*.1f), "ENTRADA", "Box"))
{
if(!clockedIn && clockedOut)
{
var clockIn = Functions.clockIn();
debugLog11 = "\nHora de entrada";
debugLog12 = "\n"+clockIn[3]+":"+clockIn[4]+":"+clockIn[5];
clockedIn = true;
clockedOut = false;
}
else
{
debugLog11 = "\n"+errorMsg1;
debugLog12 = "\n";
}
}
if(GUI.Button(Rect(width - buttonTopHMargin - width*.24f, height*.1f + buttonTopVMargin, width*.24f, height*.1f), "SALIDA", "Box"))
{
if(!clockedOut && clockedIn)
{
// debugLog11 = clockedIn+"\n"+clockedOut;
Functions.clockOut();
var clockOut = Functions.clockOut();
var workedSecondsToday : Array = Functions.calculateDay();
var workedTimeToday = Functions.convertSeconds(parseInt(workedSecondsToday[0].ToString()));
var extraTimeToday = Functions.convertSeconds(parseInt(workedSecondsToday[1].ToString()));
var festTimeToday = Functions.convertSeconds(parseInt(workedSecondsToday[2].ToString()));
if(parseInt(workedSecondsToday[0].ToString()) > 0 && parseInt(workedSecondsToday[1].ToString()) < 1 && parseInt(workedSecondsToday[2].ToString()) < 1)
{
debugLog11 = "\nHora de Salida\nNormal:"; //NORMAL
debugLog12 = "\n"+clockOut[3]+":"+clockOut[4]+":"+clockOut[5]+"\n"+workedTimeToday[1]+":"+workedTimeToday[2]+workedTimeToday[3];
}
else if(parseInt(workedSecondsToday[0].ToString()) > 0 && parseInt(workedSecondsToday[1].ToString()) > 0 && parseInt(workedSecondsToday[2].ToString()) < 1)
{
debugLog11 = "\nHora de Salida\nNormal:\nExtra:"; //NORMAL + EXTRA
debugLog12 = "\n"+clockOut[3]+":"+clockOut[4]+":"+clockOut[5]+"\n"+workedTimeToday[1]+":"+workedTimeToday[2]+workedTimeToday[3]+"\n"+extraTimeToday[0]+"-"+extraTimeToday[1]+":"+extraTimeToday[2]+":"+extraTimeToday[3];;
}
else if(parseInt(workedSecondsToday[0].ToString()) > 0 && parseInt(workedSecondsToday[1].ToString()) < 1 && parseInt(workedSecondsToday[2].ToString()) > 0)
{
debugLog11 = "\nHora de Salida\nNormal:\nFestivo:"; //NORMAL + FESTIVO
debugLog12 = "\n"+clockOut[3]+":"+clockOut[4]+":"+clockOut[5]+"\n"+workedTimeToday[1]+":"+workedTimeToday[2]+workedTimeToday[3]+"\n"+festTimeToday[0]+"-"+festTimeToday[1]+":"+festTimeToday[2]+":"+festTimeToday[3];
}
else if(parseInt(workedSecondsToday[0].ToString()) > 0 && parseInt(workedSecondsToday[1].ToString()) > 0 && parseInt(workedSecondsToday[2].ToString()) > 0)
{
debugLog11 = "\nHora de Salida\nNormal:\nExtra:\nFestivo:"; //NORMAL + EXTRA + FESTIVO
debugLog12 = "\n"+clockOut[3]+":"+clockOut[4]+":"+clockOut[5]+"\n"+workedTimeToday[1]+":"+workedTimeToday[2]+workedTimeToday[3]+"\n"+extraTimeToday[0]+"-"+extraTimeToday[1]+":"+extraTimeToday[2]+":"+extraTimeToday[3]+"\n"+festTimeToday[0]+"-"+festTimeToday[1]+":"+festTimeToday[2]+":"+festTimeToday[3];
}
clockedOut = true;
clockedIn = false;
}
else
{
debugLog01 = mainMsg;
debugLog11 = "\n"+errorMsg2;
debugLog12 = "\n";
}
}
Can't explain myself better since I have no clue about what is happening. Any help will be much appreciated.
Yesterday, i started placing tons of labels everywhere in my code and finally discovered where it stopped.
It was just replacing this...
var sw = new StreamWriter(Application.persistentDataPath+"\\Settings.txt", false);
by...
var sw = new StreamWriter(Application.persistentDataPath+"/Settings.txt", false);
Only windows supports this bar "\" when used for setting paths, but i was thinking all the time that the error was somewhere in the OnGUI function.
Thanks lot to everyone who came helping me :)
On the Unity3D you can find this information:
Work with specific constraints (constraint):
Samsung Galaxy S (30 MB limit to downloadable file size, also problems with UI)
Samsung Galaxy Tab (30 MB limit to downloadable file size)
HTC Desire (40 MB limit to downloadable file size)
Samsung Galaxy S2 (Minor but possibly annoying issue: phone may work in 16-bit mode with Unity and show color banding Edit: a workaround was added to address the GS2 graphics driver, I don't know if it was related to this problem or not)
HTC EVO3D (Must build with Unity 3.4)
HTC Sensation (Must build with Unity 3.4)
Samsung Galaxy S2 (Minor but possibly annoying issue: phone may work in 16-bit mode with Unity and show color banding Edit: a workaround was added to address the GS2 graphics driver, I don't know if it was related to this problem or not)
Unity3D Android Limitations

Categories

Resources