Swift 4 - Jumper Game Scoring - ios

I have code in place for the scoring in a jumper game that I am creating. The score increases as the player moves up the screen. This code below works fine for keeping track of the score but the resulting Int is very high. Thus, a high score would result in the millions. I am trying to figure out how to divide the scoring increments, so that a high score would result in the thousands instead of the millions.
In line two, I tried putting / 2 after after ...position.y, but that resulted in an extremely low negative Int.
Any suggestions?
if Int(player.position.y) > maxPlayerY! {
GameState.sharedInstance.score += Int(player.position.y) - maxPlayerY!
maxPlayerY = Int(player.position.y)
scoreLabel.text = String(format: "Score: %d", GameState.sharedInstance.score)
}

Related

iOS Detect aggressive behavior of a vehicle driver

I am working on a driver behavior app and I am using SOMotionDetector (Thanks to MIT). Its giving speed and Motion Type (Not Moving, Walking, Running, Automotive) of device. I will use Automotive in my case as I need to detect driver behavior. This is detecting Motion Type based on speed with some thresholds set for Walking, Running, Automotive or if available it uses M7 Chip. It updates location approximately after every second (time varies based on GPS) in [SOMotionDetector sharedInstance].locationChangedBlock To detect Aggressive speed or break I am checking is that the increase/decrease of speed in last second. If it increases from a certain threshold (I am using kAggressiveSpeedIncrementFactor 8.0f) then its aggressively increasing speed, and if there is decreasing speed (difference factor is negative in this case) then its aggressive break. For turn I am playing with angle of latitude and longitude points, following is code for my logic:
#define kAggressiveSpeedIncrementFactor 8.0f // if 8 km/h speed was increased in last second
#define kAggressiveAngleIncrementFactor 30.0f // 30 degree turn angle
#define kAggressiveTurnIncrementFactor 5.0f . // while turn the increasing speed factor in last second
SOMotionDetector *motionDetector = [SOMotionDetector sharedInstance];
motionDetector.locationChangedBlock = ^(CLLocation *location) {
if (motionDetector.motionType == MotionTypeAutomotive) {
SOLocationManager *locationManager = [SOLocationManager sharedInstance];
float currSpeed = motionDetector.currentSpeed * 3.6f;
float lastSpeed = motionDetector.lastSpeed * 3.6f;
float currAngle = locationManager.currAngle;
float lastAngle = locationManager.lastAngle;
self.speedDiff = currSpeed-lastSpeed;
self.angleDiff = currAngle-lastAngle;
if (fabs(self.speedDiff)>kAggressiveSpeedIncrementFactor && fabs(self.angleDiff)<kAggressiveAngleIncrementFactor) {
NSString *msg = #"Aggressive Speed";
if (self.speedDiff < 0)
msg = #"Aggressive Break";
NSLog(#"%#", msg);
}
if (fabs(self.angleDiff)>kAggressiveAngleIncrementFactor && currSpeed>kAggressiveTurnIncrementFactor) {
NSLog(#"aggressive turn");
}
}
};
I have created currentSpeed and lastSpeed in SOMotionDetector class (for my speed difference) and currAngle and lastAngle in SOLocationManager. Please have a look at code,
Aggressive Speed some times work perfect
My question is:
Is this right approach what I am doing?
For detecting aggressive turn with the angle some times this happens that if
my vehicle is going 50 degrees angle (calculated with current and last lat, longs) on a strait road, some times the GPS detect location right or left side of road that give a big difference to the angle (like the path becomes a zig zag). any suggestion for this?

How to increase time of accuracy calculation?

Interesting case: if we move iPhone to iBeacon device, value of accuracy changed much faster than when we move iPhone from device.
How can I make this process faster?
As you have noted, CoreLocation averages past signal measurements to come up with an accuracy calculation (distance estimate in meters). The algorithm used to do the averaging is unpublished, but my measurements have shown a lag that stabilizes after about 20 seconds. I have not noticed a difference in lag between getting closer or further away.
You have no control over this averaging interval. The only thing you can do is average RSSI yourself over whatever time period you wish. You can then use a custom calculation to convert average RSSI to distance.
To do this you will need to have beacons that all have identical transmitter power, as you will not have access to the measured power calibration constant in the beacon advertisement. (Apple does not allow reading this value.). Instead, this constant must be hard coded in your customer distance calculation.
You can see sample code that does this here:
+(double) distanceForRSSI:(double)rssi forPower:(int)txPower {
// use coefficient values from spreadsheet for iPhone 4S
double coefficient1 = 2.922026; // multiplier
double coefficient2 = 6.672908; // power
double coefficient3 = -1.767203; // intercept
if (rssi == 0) {
return -1.0; // if we cannot determine accuracy, return -1.0
}
double ratio = rssi*1.0/txPower;
double distance;
if (ratio < 1.0) {
distance = pow(ratio,10);
}
else {
distance = (coefficient1)*pow(ratio,coefficient2) + coefficient3;
}
if (distance < 0.1) {
NSLog(#"Low distance");
}
return distance;
}
https://github.com/AltBeacon/ios-beacon-tools/blob/master/ios-beacon-tools/RNLBeacon%2BDistance.m

Speed up game after every 50 points

I am making a 2D reaction game with sprite-kit and I am having the same problem again. I already asked this question once and had good answers and everything worked finde but now I am stuck again.
Like the title says I want to speed up my game every 50 points, but it just speeds up when I get the right point number like 50, 100, 150.. The problem is that I have combo points and its always some points more. For example from 48 to 51 points, so it never speeds up. How can I speed up the game even its some points more ? I mean from 50 to 100 one speed up and from 100 to 150 and so on. Here is my code so far:
if (points % 10 == 0) {
if (readyToSpeed) {
speed++;
NSLog(#"speed up");
readyToSpeed = NO;
}
}
Thanks for the help ! (code in objective-c please)
EDIT: Works perfectly using both answers combined.
Instead of incrementing your speed you could do it like so:
speed = baseSpeed + (points / 50);
Where baseSpeed is the speed at the start of the game.
Don't worry about the exact 50x multiple points, keep track of the current point "group" via modulo to derive your speed value
cur_speed = points - (points % 50);
e.g. if they're at 203 points, then cur_speed is 203-(203%50) -> 203-3 -> 200. If they suddenly jump to 308 pts because they hit an insane points combo, then the speed becomes 308-(308%50) -> 308-8 -> 300
if you want just between 50-100 and 100-150 do something like this:
If points > 50 && points < 100 {
//speedup code
}else if points > 100 && points < 150 {
//speedup code
} //etc
Update: if you want this to be endless do this:
var speednumber = points/50
speednumber = speednumber-decimal //some code to cut of the decimals
setSpeedTo(speednumber+1) //you got to make a function for this
setSpeedTo(1) will be normal
2 will be from 50-100
3 100-150
Etc.

Game: ScoreNumber does not increase by 1. It increases by 4 every time (sometimes, 10)

I have a game where you bounce on platforms. I want the score to increase by 1 every time you bounce on a platform. However, it keeps increasing by some random number every time (like 10).
In Game.h I have:
int ScoreNumber;
IBOutlet UILabel *ScoreLabel;
-(void)Score;
...
In Game.m I have:
-(void)Score {
ScoreNumber = ScoreNumber +1.0;
ScoreLabel.text = [NSString stringWithFormat:#"%i", ScoreNumber];
}
-(void)PlatformMoving {
if (CGRectIntersectsRect(Bird.frame, Platform1.frame)) {
[self Score];
}
}
...
This should count by 1 every time but it counts by more than 1
If you place a breakpoint in your (void)Score method you'll probably see that its being called multiple times per bounce because the rectangles probably overlap for multiple frames. You could place a check inside the (void)PlatformMoving method to see if the last test failed before calling Score again. This would ensure that Score is never called on 2 consecutive frames and should prevent scoring any more than one point per bounce.

How to add 1 to the score each time an object's y coordinate is equal to another object's y coordinate

i have a game where the user must move the ball in a vertical way between 2 objects , i want to have a scoring system that allows me to add 1 to the score each time the ball is equal to the 2 objects so i tried the code below but it is not working probably,it is not adding one each time the ball's y coordinates are equal to the object's y cooridnates.
if(imageview.center.y == imageview1.center.y){
int score = score + 1;
scorelabel.text = [NSString stringWithFormat:#"%i",score];
}
As I assume your imageview.centre.y will be different then imageView1.centre.y .Also you could use this method CGRectIntersection(<#CGRect r1#>, <#CGRect r2#>) and place your both frame in provided fields. So it will notify whenever there is an intersection of two frames. Code will be like -
if (CGRectIntersectsRect(self.pacman.frame, self.exit.frame))
{
//Wirte your score handling code here...
}
else
{
//Skip or do what you want...
}
Hope this might help you.
Your variable is recreated every time your if is performing.
Define it somewhere before #implementation
int score = 0;
And replace int score = score + 1; with score++
As comment you should be aware of comparison of double. See This Question for details

Resources