Objective-c Making levels for an iPhone game - ios
I have a question game in which I ask the user some questions and depending on how fast the user answers correctly and what level the user is in I want the user to get different amounts of xp/ progress for the progress view that I have.
So I have a global variable called 'levelInt' which holds the value of what level the user is in (max level is 20). I also have a label that prints the the 'levelInt' variable and a progress view that shows your progress for getting into higher level/shows progress for your current level. I also have a timer which counts down from 2 minutes and depending on how fast you answer this question you get more xp/more progress for the progress view. I set it up like this:
totalSeconds is the timer
levelInt is the global variable that hold the value of the level the user is in
progressView is the progress bar the show the remaining xp until next level
and the code:
if ( levelInt == 0 && totalSeconds > 100){
progressView.progress = .60;
}else if ( levelInt == 0 && totalSeconds <= 100 ){
progressView.progress = .40;
}else if ( levelInt == 0 && totalSeconds > 80 ){
progressView.progress = .20;
}else if ( levelInt == 0 && totalSeconds <= 80 ) {
progressView.progress = .10;
}else {
progressView.progress = .05;
}
if ( levelInt == 1 && totalSeconds > 100){
progressView.progress = .55;
}else if ( levelInt == 1 && totalSeconds <= 100 ){
progressView.progress = .35;
}else if ( levelInt == 1 && totalSeconds > 80 ){
progressView.progress = .15;
}else if ( levelInt == 1 && totalSeconds <= 80 ) {
progressView.progress = .05;
}else {
progressView.progress = .02;
}
if ( levelInt == 2 && totalSeconds > 100){
progressView.progress = .50;
}else if ( levelInt == 2 && totalSeconds <= 100 ){
progressView.progress = .30;
}else if ( levelInt == 2 && totalSeconds > 80 ){
progressView.progress = .10;
}else if ( levelInt == 2 && totalSeconds <= 80 ) {
progressView.progress = .05;
}else {
progressView.progress = .02;
}
if ( levelInt == 3 && totalSeconds > 100){
progressView.progress = .45;
}else if ( levelInt == 3 && totalSeconds <= 100 ){
progressView.progress = .25;
}else if ( levelInt == 3 && totalSeconds > 80 ){
progressView.progress = .05;
}else if ( levelInt == 3 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 4 && totalSeconds > 100){
progressView.progress = .40;
}else if ( levelInt == 4 && totalSeconds <= 100 ){
progressView.progress = .20;
}else if ( levelInt == 4 && totalSeconds > 80 ){
progressView.progress = .05;
}else if ( levelInt == 4 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 5 && totalSeconds > 100){
progressView.progress = .35;
}else if ( levelInt == 5 && totalSeconds <= 100 ){
progressView.progress = .15;
}else if ( levelInt == 5 && totalSeconds > 80 ){
progressView.progress = .05;
}else if ( levelInt == 5 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 6 && totalSeconds > 100){
progressView.progress = .30;
}else if ( levelInt == 6 && totalSeconds <= 100 ){
progressView.progress = .10;
}else if ( levelInt == 6 && totalSeconds > 80 ){
progressView.progress = .05;
}else if ( levelInt == 6 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 7 && totalSeconds > 100){
progressView.progress = .28;
}else if ( levelInt == 7 && totalSeconds <= 100 ){
progressView.progress = .08;
}else if ( levelInt == 7 && totalSeconds > 80 ){
progressView.progress = .04;
}else if ( levelInt == 7 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 8 && totalSeconds > 100){
progressView.progress = .26;
}else if ( levelInt == 8 && totalSeconds <= 100 ){
progressView.progress = .07;
}else if ( levelInt == 8 && totalSeconds > 80 ){
progressView.progress = .05;
}else if ( levelInt == 8 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 9 && totalSeconds > 100){
progressView.progress = .24;
}else if ( levelInt == 9 && totalSeconds <= 100 ){
progressView.progress = .05;
}else if ( levelInt == 9 && totalSeconds > 80 ){
progressView.progress = .03;
}else if ( levelInt == 9 && totalSeconds <= 80 ) {
progressView.progress = .02;
}else {
progressView.progress = .02;
}
if ( levelInt == 10 && totalSeconds > 100){
progressView.progress = .22;
}else if ( levelInt == 10 && totalSeconds <= 100 ){
progressView.progress = .04;
}else if ( levelInt == 10 && totalSeconds > 80 ){
progressView.progress = .03;
}else if ( levelInt == 10 && totalSeconds <= 80 ) {
progressView.progress = .02;
}else {
progressView.progress = .02;
}
if ( levelInt == 11 && totalSeconds > 100){
progressView.progress = .20;
}else if ( levelInt == 11 && totalSeconds <= 100 ){
progressView.progress = .04;
}else if ( levelInt == 11 && totalSeconds > 80 ){
progressView.progress = .03;
}else if ( levelInt == 11 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 12 && totalSeconds > 100){
progressView.progress = .18;
}else if ( levelInt == 12 && totalSeconds <= 100 ){
progressView.progress = .04;
}else if ( levelInt == 12 && totalSeconds > 80 ){
progressView.progress = .03;
}else if ( levelInt == 12 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 13 && totalSeconds > 100){
progressView.progress = .16;
}else if ( levelInt == 13 && totalSeconds <= 100 ){
progressView.progress = .04;
}else if ( levelInt == 13 && totalSeconds > 80 ){
progressView.progress = .03;
}else if ( levelInt == 13 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 14 && totalSeconds > 100){
progressView.progress = .14;
}else if ( levelInt == 14 && totalSeconds <= 100 ){
progressView.progress = .04;
}else if ( levelInt == 14 && totalSeconds > 80 ){
progressView.progress = .03;
}else if ( levelInt == 14 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 15 && totalSeconds > 100){
progressView.progress = .12;
}else if ( levelInt == 15 && totalSeconds <= 100 ){
progressView.progress = .03;
}else if ( levelInt == 15 && totalSeconds > 80 ){
progressView.progress = .03;
}else if ( levelInt == 15 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 16 && totalSeconds > 100){
progressView.progress = .10;
}else if ( levelInt == 16 && totalSeconds <= 100 ){
progressView.progress = .03;
}else if ( levelInt == 16 && totalSeconds > 80 ){
progressView.progress = .03;
}else if ( levelInt == 16 && totalSeconds <= 80 ) {
progressView.progress = .03;
}else {
progressView.progress = .02;
}
if ( levelInt == 17 && totalSeconds > 100){
progressView.progress = .08;
}else if ( levelInt == 17 && totalSeconds <= 100 ){
progressView.progress = .02;
}else if ( levelInt == 17 && totalSeconds > 80 ){
progressView.progress = .02;
}else if ( levelInt == 17 && totalSeconds <= 80 ) {
progressView.progress = .02;
}else {
progressView.progress = .02;
}
if ( levelInt == 18 && totalSeconds > 100){
progressView.progress = .06;
}else if ( levelInt == 18 && totalSeconds <= 100 ){
progressView.progress = .02;
}else if ( levelInt == 18 && totalSeconds > 80 ){
progressView.progress = .02;
}else if ( levelInt == 18 && totalSeconds <= 80 ) {
progressView.progress = .02;
}else {
progressView.progress = .02;
}
if ( levelInt == 19 && totalSeconds > 100){
progressView.progress = .04;
}else if ( levelInt == 19 && totalSeconds <= 100 ){
progressView.progress = .02;
}else if ( levelInt == 19 && totalSeconds > 80 ){
progressView.progress = .02;
}else if ( levelInt == 19 && totalSeconds <= 80 ) {
progressView.progress = .02;
}else {
progressView.progress = .02;
}
if ( levelInt ==20 && totalSeconds > 100){
progressView.progress = .04;
}else if ( levelInt == 20 && totalSeconds <= 100 ){
progressView.progress = .02;
}else if ( levelInt == 20 && totalSeconds > 80 ){
progressView.progress = .02;
}else if ( levelInt == 20 && totalSeconds <= 80 ) {
progressView.progress = .02;
}else {
progressView.progress = .02;
}
The problem is that whenever I answer correctly fast then the 20 seconds, the progress view only adds about .10. Should I do progressView.progress = progressView.progress + (amount)?
The first or the second statement will always validates true,
therefor the last three if statements will never be validated.
Numbers cannot validate false to both <= 100 and > 100.
Numbers validating false to > 100 are always <= 100.
if ( i > 100 )
{
// All numbers above 100
}
else if ( i <= 100 )
{
// All numbers lower than or equal to 100
}
else if ( i > 80 )
{
// Never reached
}
else if ( i <= 80 )
{
// Never reached
}
else
{
// Never reached
}
2 better ways to do the job :
1 : create a function (linear ?) to compute your progress.
float progress (int level , int seconds);
2 : use an array (your read from a file for better use)
//float values [levels][cases];
//float values [21][3];
static init
float values[0] = {.10,.20,.60};
float values[1] = ....
int acase ;
if (totalSeconds <= 80){
acase=0;
}else{
// your are > 80 here
if (totalSeconds <= 100){
acase=1;
}else{
// your are > 100 here
acase=2;
}
}
progressView.progress = values[levelInt,acase];
and it's done (more shorter i think)
you can of course add cases (here 3)
Related
How do I correct my code to enable my Expect Advisor to take trades?
My EA was taking only 1 trade at a time on a single currency pair, it ignores the other pairs till the current trade is closed. I decided to modify it. Now it's no more trading at all. int Hour = TimeHour(TimeCurrent()); int DayOfWeek = DayOfWeek(); int total = OrdersTotal(); int count=0; for(int i=0; i<total; i++) { if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol() == _Symbol && OrderMagicNumber()==MagicNumber) { Alert("Inside Order Select if"); // Checking if spread is less than 2.0 pips. if(current_spread <= AllowableSpread) { // Checking for days of the week if(DayOfWeek >= 1 && DayOfWeek <= 5) { // Checking for time of the day if(Hour >= 3 && Hour <= 20) { if(ADXValue > 25) { if(RSIValue > 50 || RSIValue < 20) { if(PreviousFast<PreviousSlow && CurrentFast > CurrentSlow) ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, Ask-(StopLoss*pips), Ask+(TakeProfit*pips), NULL, MagicNumber, 0, Green); if(ticket<0) Print("OrderSend failed with error #",GetLastError()); else Print("OrderSend placed successfully"); } if(RSIValue < 50 || RSIValue > 70) { if(PreviousFast>PreviousSlow && CurrentFast < CurrentSlow) ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, Slippage, Bid+(StopLoss*pips), Bid-(TakeProfit*pips), NULL, MagicNumber, 0, Red); if(ticket<0) Print("OrderSend failed with error #",GetLastError()); else Print("OrderSend placed successfully"); } } }
MQL4 my first EA overtrades if I increase the amount of conditions it calculates
I have created an expert advisor that calculates some conditions and gives a value either long or short for each condition (+or- 0.1 lots), it then sums all the conditions to give a net long or short position. It should then either open a new order or close an open one to alter the previous open positions(mlots) to match the target value the trade conditions have calculated(sum) on the change of each new bar; this is done by using the ontick function. If I use only one or two conditions it works fine, but when I increase the number of conditions (sum can be greater than 0.2 lots or less than -0.2lots) it over trades by one and then closes the position which it should and then over trades by one again which it shouldn't get in to a nasty cycle. I don't understand why it would work perfectly for two conditions but not for more than two I wonder if anyone has an idea I would be grateful for any input. This is my first attempt at an EA any thoughts would be greatly appreciated best regards Ken //+------------------------------------------------------------------+ //| test.mq4 | //| Copyright 2018, K T | //| https:// | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, K T" #property link "" #property version "1.00" #property strict #property description "SIMPLE TRADER" //INPUT PARAMETERS input double lots=0.1; input int MagicNumber=1111;//MAGICNO MUST BE UNIQUE input int condno=1; //1=3/10 10 2=10 10/20 3=ALL input double test=0.0; input int spread=1; input int slippage=10; input string simble="FTSE100(£)"; input int period=PERIOD_M1; input double TP=200; input double SL=100; input int ma1input=3; input int ma2input=10; input int ma3input=20; input int sleep=0; //ONINIT TEST FOR CORRECR CONDITIONS int OnInit() { string simble1=simble; int period1=period; if (simble1==Symbol()&& period1==Period() && Ask-Bid<=spread) { Alert ("CORRECT SYMBOL ", simble," mlots=", mlots(),"sum= ",sum()); Alert ("CORRECT TIME T", period); return(INIT_SUCCEEDED); } else { Alert ("INIT FAILED"); Alert ("wrong symbol, time or spread too wide"); return(INIT_FAILED); } } //ON EVERY TICK void OnTick() { double sum1 = sum(); double mlots1 = mlots(); if(sum1 == mlots1) { neutral(); return; } else if(sum1>mlots1 && mlots1>=0 && mlots1!= sum1) { openbuy(); return; } else if(sum1>mlots1 && mlots1<0 && mlots1!= sum1) { closesell(); return; } else if(sum1<mlots1 && mlots1<=0 && mlots1!= sum1) { opensell(); return; } else if(sum1<mlots1 && mlots1>0 && mlots1!= sum1) { closebuy(); return; } else return; } //NEUTRAL void neutral() { Alert("T", period, " EQUILIBRIUM ", simble,"/ sum=",sum(),"/ mlots=" ,mlots()); return; } //OPEN BUY void openbuy() { double TakeProfitLevel; double StopLossLevel; TakeProfitLevel = Bid + TP*Point*10; //0.00001 * 10 = 0.0001 StopLossLevel = Bid - SL*Point*10; if(mlots()!= sum()) { OrderSend(simble, OP_BUY, lots, Ask, slippage*10, StopLossLevel, TakeProfitLevel, "BUY", MagicNumber);//notice that slippage also has to be multiplied by 10 Alert(MagicNumber," T", period, " OPENBUYFCTION = ", simble,"/ sum=",sum(),"/ mlots=" ,mlots()); Sleep(sleep); return; } else { return; } } //OPEN SELL void opensell() { double TakeProfitLevel; double StopLossLevel; //here we are assuming that the TakeProfit and StopLoss are entered in Pips TakeProfitLevel = Ask - TP*Point*10; //0.00001 * 10 = 0.0001 StopLossLevel = Ask + SL*Point*10; if (mlots()!= sum()) { OrderSend(simble, OP_SELL, lots, Bid, slippage*10, StopLossLevel, TakeProfitLevel, "SELL", MagicNumber); //notice that slippage also has to be multiplied by 10 Alert(MagicNumber," T", period, " OPENSELLFCTION = ", simble,"/ sum=",sum(),"/ mlots=" ,mlots()); Sleep(sleep); return; } else { return; } } //CLOSE BUY void closebuy() { if(mlots()==sum()) { neutral(); return; } int low=OrderTicket(); int i=OrdersTotal(); for (i = OrdersTotal(); i >=0; i--) { if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue; if (i<=OrdersTotal() && OrderType()== OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber ) bool closed = OrderClose( low, OrderLots(), Bid, slippage, White); Alert("Ticket= ", low); Alert(MagicNumber," T", period, " CLOSEBUYFCTION = ", simble,"/ sum=",sum(),"/ mlots=" ,mlots()); Sleep(sleep); return; } } //CLOSE SELL void closesell() { if(mlots()==sum())neutral(); int low=OrderTicket(); int i=OrdersTotal(); for (i = OrdersTotal(); i >=0; i--) { if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue; if (i<=OrdersTotal() && OrderType()== OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber ) bool closed = OrderClose( low, OrderLots(), Ask, slippage, White); Alert("Ticket= ", low); Alert(MagicNumber," T", period, " CLOSESELLFCTION = ", simble,"/ sum=",sum(),"/ mlots=" ,mlots()); Sleep(sleep); return; } } //CONDITION 1 double cond1() //cond 1 3ema 10ma { double ma1; double ma2; ma1=iMA(NULL,0,ma1input,0,MODE_EMA,PRICE_CLOSE,1);//3EMA ma2=iMA(NULL,0,ma2input,0,MODE_SMA,PRICE_CLOSE,1);//10MA if(ma1>=ma2) { return(lots); } else { return(lots*-1); } } //CONDITION 2 double cond2() //cond 2 10ema 10ma { double ma1; double ma2; ma1=iMA(NULL,0,ma1input,0,MODE_EMA,PRICE_CLOSE,1);//3EMA ma2=iMA(NULL,0,ma2input,0,MODE_EMA,PRICE_CLOSE,1);//10EMA if(ma1>=ma2) { return(lots); } else { return(lots*-1); } } //CONDITION 3 double cond3() //cond 3 10ema 20ema { double ma1; double ma2; ma1=iMA(NULL,0,ma2input,0,MODE_EMA,PRICE_CLOSE,1);//10EMA ma2=iMA(NULL,0,ma3input,0,MODE_EMA,PRICE_CLOSE,1);//20EMA if(ma1>=ma2) { return(lots); } else { return(lots*-1); } } //CONDITION 4 double cond4() //cond 4 10ma 20Ema { double ma1; double ma2; ma1=iMA(NULL,0,ma2input,0,MODE_SMA,PRICE_CLOSE,1);//10MA ma2=iMA(NULL,0,ma3input,0,MODE_EMA,PRICE_CLOSE,1);//20EMA if(ma1>=ma2) { return(lots); } else { return(lots*-1); } } //SELECTING CONDITIONS TO BE INCLUDED double sum() { if(condno==1) { return(cond1() + cond2()); } else if(condno==2) { return(cond3() + cond4()); } else if (condno==3) { return(cond1() + cond2()+ cond3() + cond4() );//IF MORE THAN TWO CONDITIONS USED IT OVERTRADES B ONE AND THEN CLOSES ERROR TRADE } else return(cond1() + cond2()); } //COUNT OF ALL OPEN TRADES double mlots() { double mlots1 = BuyTotalMagicOpen() + SellTotalMagicOpen(); double mlots2 = mlots1/10; return (mlots2); } //COUNT BUY ORDERS double BuyTotalMagicOpen() { int OrderCount = 0; for (int i = OrdersTotal() - 1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue; if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) if (OrderType() == OP_BUY) OrderCount++; } return (OrderCount); } //COUNT SELL ORDERS double SellTotalMagicOpen() { int OrderCount = 0; for (int i = OrdersTotal() - 1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue; if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) if (OrderType() == OP_SELL) OrderCount++; } return (OrderCount*-1); } //CLOSE ALL MAGICNUMBER REFRESHRATES BUSYSLEEP MODE void CloseThis() { for (int i = OrdersTotal(); i >=0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); while(IsTradeContextBusy()) Sleep(100); RefreshRates(); if (OrderType() == OP_BUY && Symbol() == OrderSymbol() && MagicNumber == OrderMagicNumber()) { bool closed = OrderClose( OrderTicket(), OrderLots(), Bid, slippage, Red); Alert("T", period, " BUY CLOSED ON DEINIT"); } if (OrderType() == OP_SELL && Symbol() == OrderSymbol() && MagicNumber == OrderMagicNumber()) { bool closed = OrderClose( OrderTicket(), OrderLots(), Ask, slippage, Green); Alert("T", period, " SELL CLOSED ON DEINIT"); } } } //DEINIT void OnDeinit(const int reason)//MAGIC NO { CloseThis(); Alert("T", period, " DEINIT SUCCEEDED"); } //
how do i calculate user inputs in form?
i have a rails app with user inputs, i have to perform a calculation and store its new value. eg of price is given which was earlier written in js and php if(count($_POST['price']) >= 0 && !empty($_POST['price'])) { $price = $_POST['price']; $sql .= " '". $price ."', "; switch ($price){ case ($price >= 0 && $price <= 21): $price_rating = 0; break; case ($price >= 22 && $price <= 29): $price_rating = 5; break; case ($price >= 30 && $price <= 39): $price_rating = 7; break; case ($price >= 40 && $price <= 49): $price_rating = 8; break; case ($price >= 50 && $price <= 59): $price_rating = 6; break; default: $price_rating = 0; } } else { $price_rating = 0; $sql .= " '', "; }
everything is the same... in the according controller you just need to do #price = params[:price] (which is the same as $price = $_POST['price'];) if (0..21).include?(#price) #price_rating = 0 if ... and so on
if conditional of number and its increment by 16 in iOS [duplicate]
This question already has answers here: Consecutive Number After 16 in iOS (4 answers) Closed 9 years ago. i have an integer called count , and i want it when it comes 1 or 17 or 33 to forever by the same sequence i.e count += 16 the first ball be unhidden , and the same thing when count comes to 2 or 18 or 34 to forever by the same sequence i.e count += 16 , the second ball to unhidden at first i typed this code but it is very hard to type all numbers with their increments to the end in if condition if(count == 1 || count == 17 || count == 33 || count == 49 || count == 65 || count == 81 || count == 97 || count == 113 || count == 129 || count == 145 || count == 161 || count == 177 || count == 193 || count == 209 || count == 225 || count == 241) { _firstBall.hidden = NO; } if(count == 2 || count == 18 || count == 34 || count == 50 || count == 66 || count == 82 || count == 98 || count == 114 || count == 130 || count == 146 || count == 162 || count == 178 || count == 194 || count == 210 || count == 226 || count == 242) { _secondBall.hidden = NO; }
Try this: if( ((count-1) % 16) == 0) { _first.hidden = NO; } if( ((count-2) % 16) == 0) { _secondBall.hidden = NO; }
The % operator returns the integer remainder (or modulus). Your first sequence: 1, 17, 33 is 16 * n + 1 so the remainder when divided by 16 is 1. Similarly your second second sequence is 16 * n + 2. So you can: switch (count % 16) // switch on the remainder of division by 16 { case 1: _firstBall.hidden = NO; break; case 2: _secondBall.hidden = NO; break; // add more cases if needed }
Getting CLLocation course and translate to user
I'm trying to get information about the direction taken by the user (N-S-W-O) via CLLocation's course method. I'm using this code: double course; bool isNord; course = currentLocation.course; NSLog(#"Current course: %f", course); NSInteger courseString = course; //_labelCompass.text = [NSString stringWithFormat:#"%d", courseString]; if (courseString == -1) { _labelCompass.text = #"N/A"; } if (courseString >= 22 && courseString <= 67) { isNord = false; _labelCompass.text = #"NE"; } if (courseString >=67 && courseString <= 112) { isNord = false; _labelCompass.text = #"E"; } if (courseString >= 112 && courseString <= 157) { isNord = false; _labelCompass.text = #"SE"; } if (courseString >= 157 && courseString <= 208) { isNord = false; _labelCompass.text = #"S"; } if (courseString >= 208 && courseString <= 247) { isNord = false; _labelCompass.text = #"SW"; } if (courseString >= 247 && courseString <= 292) { isNord = false; _labelCompass.text = #"W"; } if (courseString >= 292 && courseString <= 337) { isNord = false; _labelCompass.text = #"NW"; } if (isNord == true) { _labelCompass.text = #"N"; } Of course I'm putting this into - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation and the course information is update successfully. Using this algorithm I cannot know when the course is North like I do for the others so I've built this. There are any other methods better than this? Thanks a lot.
The wrong thing is that with <= 67 of the first if and the second >= 67 of the second if, if the value is 67, ALWAYS the first if will be triggered. Also, as stated by rmaddy please use else if, it's kind more efficent. Finally use the best syntax min < myVal && myVal < max to emulate the Mathematical range min < MyVal < max.
Why can't you determine of the course is North? Just do: if (course < 22 || course > 337) { _labelCompass.text = #"N"; } BTW - please use else if. It will be far more efficient: if (courseString == -1) { _labelCompass.text = #"N/A"; } else if (courseString >= 22 && courseString <= 67) { _labelCompass.text = #"NE"; } else if (courseString >=67 && courseString <= 112) {
CLLocationManager has an heading option with an heading update Delegate. - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading newHeading has a property 'magneticHeading': 0 is north, 90 is east, 180 is south, 270 is west