Operator && cannot be applied to operands of type 'bool' and 'TouchPhase' - touch

Noob here... I am trying to understand why my compiler complains about this:
if(Input.touchCount >0 && Input.GetTouch(0).phase = TouchPhase.Moved) { ... }
I am following the tutorial here, at 10:00:
https://youtu.be/ikxcj55BBuE?t=598

Add a = to Input.GetTouch(0).phase = TouchPhase.Moved).
This is how your code should look:
if(Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Moved)

Related

New to Swift (and programming in general). Why is my code calling the wrong action?

I'm a total newbie, so pardon me if this is an annoying question. I am just practicing and am wondering why this code is printing "Almost perfect. Give it another try!" when it should be printing "Well, you got one correct". Any help would be much appreciated
var subjectOne = true
var subjectTwo = false
var subjectThree: Int = 5
func shout(subjectOne: Bool, subjectTwo: Bool, subjectThree: Int){
if subjectOne && subjectTwo && subjectThree >= 25{
print("HIGH SCORE!")
}
else if subjectOne || subjectTwo && subjectThree >= 10{
print("Almost perfect. Give it another try!")
}
else if subjectOne && subjectTwo && subjectThree >= 10{
print("There ya' go!")
}else if !subjectOne && !subjectTwo && subjectThree >= 10{
print("Well, you got a high enough number at least!")
}else if subjectOne || subjectTwo && subjectThree < 10{
print("Well, you got one correct")
}else if !subjectOne && !subjectTwo && subjectThree < 10{
print("You fail at life!")
}else if subjectOne || subjectTwo && subjectThree >= 25{
print("Almost a high score. Keep going, you got this!")
}
}
shout(subjectOne: subjectOne, subjectTwo: subjectTwo, subjectThree: subjectThree)
This is happening because your first else if clause is evaluating to true due to order of operations:
else if subjectOne || subjectTwo && subjectThree >= 10
evaluates from left to right, so your code first checks if subjectOne is true. Because it is indeed true, and an || (or) comes next, your code no longer needs to evaluate the rest of the expression because it already evaluates as true.
You might have meant to write that clause this way instead:
else if (subjectOne || subjectTwo) && subjectThree >= 10
This would evaluate to false because thanks to the parenthesis, your code knows that it must evaluate both sides of the expression (subjectOne or subjectTwo) AND subjectThree must be greater than or equal to ten.

The operator '>' isn't defined for the type 'String'. Try defining the operator '>'. error in dart

i wrote this code :
void main() {
int kian = 40;
if (kian > 10) {
print('mamad');
}
}
and now for run I have this error :
The operator '>' isn't defined for the type 'String'.
Try defining the operator '>'.
how can i fix it?
i will be so happy if you answer me :)
You were trying to compare two variables which were of type String.
void main() {
String kian = '40';
if (kian > '10') {
print('mamad');
}
}
//Error message: line 3 • The operator '>'
// isn't defined for the type 'String'.
//Try defining the operator '>'.
However, in your question you provided a running code. Check it here.
With the code, it working fine cause it type is int. The error may throw from another place.
In case your variable is String of number like this String kian = '40'; you can write your custom operator in a extension to able to compare them but not recommend. Except you are 100% sure it is an number.
void main() {
try{
print("'40' > '10': ${'40' > '10'}");
print("'40' < '10': ${'40' < '10'}");
print("'40' >= '10': ${'40' >= '10'}");
print("'40' <= '10': ${'40' <= '10'}");
}catch(e){
print('error. the string is not a number');
}
}
extension on String {
operator >(String other){
return double.parse(this) > double.parse(other);
}
operator >=(String other){
return double.parse(this) >= double.parse(other);
}
operator <(String other){
return double.parse(this) < double.parse(other);
}
operator <=(String other){
return double.parse(this) <= double.parse(other);
}
}
Result
'40' > '10': true
'40' < '10': false
'40' >= '10': true
'40' <= '10': false

Swift on array.sort - Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

I am downgrading Swift code from Xcode 8.3.1 to Xcode 7.3.1.
The Swift compiler of Xcode 7.3.1 raises
Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
while pointing on line zeroParameterAndPaths.sort {. The code was ok in Xcode 8.3.1.
What's wrong and how to fix it?
class NewConnectingSegmentZeroParameterAndPath {
let step : Int; // 0 = main, 1 = first outline, 2 = second outline
let parameter : CGFloat;
init(step: Int, parameter: CGFloat) {
self.step = step;
self.parameter = parameter;
}
}
var zeroParameterAndPaths : [NewConnectingSegmentZeroParameterAndPath] = [];
// ... some zeroParameterAndPaths .appendContentsOf calls
zeroParameterAndPaths.sort {
return $0.parameter < $1.parameter
|| ($0.parameter == $1.parameter
&& ($0.step == 1 || ($0.step == 0 && $1.step == 2))
)
};
You have two choices. One is simply to do what the error message suggests, i.e. pulling the complex bool apart into separate pieces:
zeroParameterAndPaths.sort {
let bless = ($0.parameter < $1.parameter)
let beq = ($0.parameter == $1.parameter)
let band = ($0.step == 0 && $1.step == 2)
let bor = ($0.step == 1 || band)
let beqandbor = (beq && bor)
return (bless || beqandbor)
};
The other is to provide an explicit in line giving the param types and result type:
zeroParameterAndPaths.sort {
(a:NewConnectingSegmentZeroParameterAndPath, b:NewConnectingSegmentZeroParameterAndPath) -> Bool in
return a.parameter < b.parameter
|| (a.parameter == b.parameter
&& (a.step == 1 || (a.step == 0 && b.step == 2))
)
};
You could also make your class a little bit more helpful and make it implement the condition. The compiler is much less likely to get confused in a function body than in a closure:
class NewConnectingSegmentZeroParameterAndPath {
let step : Int; // 0 = main, 1 = first outline, 2 = second outline
let parameter : CGFloat;
init(step: Int, parameter: CGFloat) {
self.step = step;
self.parameter = parameter;
}
func orderedBefore(_ other: NewConnectingSegmentZeroParameterAndPath) -> Bool
{
return parameter < other.parameter
|| parameter == other.parameter
&& (step == 1 || step == 0 && other.step == 2)
}
}
var zeroParameterAndPaths : [NewConnectingSegmentZeroParameterAndPath] = [];
// ... some zeroParameterAndPaths .appendContentsOf calls
zeroParameterAndPaths.sort { $0.orderedBefore($1) }
Apart from the issue of the type inference engine not being able to quickly resolve such complex bool expressions, such expressions are really hard to follow. I suggest you break it down into something simpler, like so:
zeroParameterAndPaths.sort {
if $0.parameter != $1.parameter { return $0.parameter < $1.parameter ]
if $0.step == 1 { return true }
if $0.step == 0 && $1.step == 2 { return true }
return false
};
There's my attempt at it. I'm not even sure if it's correct, the original expression is pretty hard to follow.

I am trying to update my for loop for Swift 3 and I can't figure it out. I keep getting errors

This is my original function in Swift 2:
// check on winning combinations
func checkWinnerMove(){
for var i = 0; i<winningCombinations.count && !isWinner;i += 1 {
if gameState[winningCombinations[i][0]-1] == activePlayer &&
gameState[winningCombinations[i][1]-1] == activePlayer &&
gameState[winningCombinations[i][2]-1] == activePlayer{
isWinner = true;
}else{
isWinner = false;
}
}
}
I have changed it to this:
// check on winning combinations
func checkWinnerMove(){
for i in 0 ..< winningCombinations.count && !isWinner{
if gameState[winningCombinations[i][0]-1] == activePlayer &&
gameState[winningCombinations[i][1]-1] == activePlayer &&
gameState[winningCombinations[i][2]-1] == activePlayer{
isWinner = true;
}else{
isWinner = false;
}
}
}
But keep getting a error when I add the
&& !isWinner
statment in the for-in loop. The error I get is:
No '..<' candidates produce the expected contextual result type 'Bool'
Any suggestions? Thank You!
Instead of forcibly trying to rewrite your original C-style for loop, consider what you're trying to achieve and attempt to re-write it in "native" Swift from scratch. How about breaking out of your loop once your true condition is met, instead of keeping it in the loop signature? E.g.
for i in 1...5 {
print(i)
if i == 3 { break }
} // 1 2 3
Applied to your example
func checkWinnerMove()
isWinner = false
for i in 0 ..< winningCombinations.count {
if gameState[winningCombinations[i][0]-1] == activePlayer &&
gameState[winningCombinations[i][1]-1] == activePlayer &&
gameState[winningCombinations[i][2]-1] == activePlayer {
isWinner = true
break
}
}
}
The explicit by index access of the (unknown for us) gameState and winningCombinations sequences is quite "unswifty" w.r.t. in the dangers of runtime exceptions for indices out of range for the sequences. So bear in mind that there are safer ways to perform such access.
For future reference, consider reading How to create a Minimal, Complete, and Verifiable example: since we (the potential answerer's of your question) don't have access to/the full information regarding isWinner, winningCombinations, gameState or activePlayer, we can't verify your example. Also, since the question cover a concept, it could be boiled down to a more minimal form. (Welcome to StackOverflow!)

Issue using .Contains with application.loadedlevel/loadlevel

I'm working on a script that returns to the correct scene when pressing escape.
In the DataUpgrade, Level Select, TransferData(x) it should return to StartMenu, in StartMenu it should close the game and in a Level(x) it should return to TransferData(x).
The following script has been made with help from FunctionR.
#pragma strict
import System;
function Update () {
if (Input.GetKey ("escape"))
{
var level : String = Application.loadedLevel.ToString();
//var transferData = "TransferData";
if(level == "StartMenu"){
Application.Quit();
}
else if(level == "DataUpgrade"){
Application.LoadLevel("StartMenu");
}
else if(level == "Level Select"){
Application.LoadLevel("StartMenu");
}
else if(level.Contains("TransferData"))
Application.LoadLevel("StartMenu");
else if(level.Contains("Level"))
Application.LoadLevel (Application.loadedLevel - 1);
//trying this out but isn't working either
/*else if( transferData in level)
Application.LoadLevel("StartMenu");
else if("Level" in level)
Application.LoadLevel (Application.loadedLevel - 1);*/
}
}
i'm not sure why it's not working, the scenes have the correct names and have also been added to build settings.
Thanks in advance
Quick Fix
This is how you fix it, and you don't need the for loop. I use Contains() to do the hard work.
import System;
function Update ()
{
if (Input.GetKey ("escape"))
{
var level : String = Application.loadedLevel.ToString();
if(level == "StartMenu")
Application.Quit();
else if(level == "DataUpgrade")
Application.LoadLevel("StartMenu");
else if(level == "Level Select")
Application.LoadLevel("StartMenu");
else if(level.Contains("TransferData")
Application.LoadLevel("StartMenu");
}
}
Smart Fix
You can also be smart and take advantage of the OR operator to reduce your related if statements.
else if(level == "DataUpgrade" || level == "Level Select" || level.Contains("TransferData")
Application.LoadLevel("StartMenu");
Sneaky Fix
You can also be sneaky since all other cases, except one, takes you back to StartMenu. You can have an else that guarantees a return to StartMenu.
if(Application.loadedLevel == "StartMenu")
Application.Quit();
else
Application.LoadLevel("StartMenu");
Replacing this line:
var level : String = Application.loadedLevel.ToString();
with
var level = Application.loadedLevelName;`
fixed the problem, everything is working now