Random Positioning issue - ios

I am trying atm doing a game. I got some problems with the RandomPositioning with the blocks. With my code, the Blocks keep spawning at the same lane very fast. But thats not what I mean to. They should appear like a Mario Game. My Code:
HumanMovement = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:#selector(HumanMoving) userInfo:nil repeats:YES];
[self PlaceBlocks];
BlockMovement = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:#selector(BlockMoving) userInfo:nil repeats:YES];
}
-(void)BlockMoving{
StartingBlock.center = CGPointMake(StartingBlock.center.x -1, StartingBlock.center.y);
Block1.center = CGPointMake(Block1.center.x -1, Block1.center.y);
Block2.center = CGPointMake(Block2.center.x -1, Block2.center.y);
if ( Block1.center.x > 20) {
[self PlaceBlocks];
}
}
-(void)PlaceBlocks{
RandomBlock1Position = arc4random() %568;
RandomBlock1Position = RandomBlock1Position + 140;
RandomBlock2Position = RandomBlock2Position + 500;
Block1.center = CGPointMake(193 , RandomBlock1Position);
Block2.center = CGPointMake(280 , RandomBlock2Position);
}

So if i understood correctly your blocks should respawn once the reach the left side. In order to do so you have to change your center check position to :
if ( Block1.center.x < 20) {
[self PlaceBlocks];
}

Related

count up from 0.00 to 176.20 with NSTimer

I am working on an app and I want to animate the numbers in my app. I know I need to use an NSTimer. Just not sure how. For example I want the app to count up from 0.00 to 176.20 (self.total.text).
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(update) userInfo:nil repeats:YES];
- (void)update{
float currentTime = [self.total.text floatValue];
float newTime = currentTime + 0.1;
self.total.text = [NSString stringWithFormat:#"%f", newTime];
}
You need to decide at what increment you want to count. You want to stop at 176.20 so it seems like an increment of 0.1 seconds would be what you want. You need a variable to store the current position.
Obj-c///
const float limit = 176.2f
#property (nonatomic) float seconds;
#property (nonatomic, strong) NSTimer *updateTimer;
// Initialize
self.seconds = 0.0f;
self.updateTimer = [NStimer scheduledTimerWithTimeInterval:0.1f target:self selector:#selector(timerFired) userInfo:nil repeats:true];
Swift///
var seconds = 0.0
let limit = 176.2
let timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("timerFired"), userInfo: nil, repeats: true)
Then you need to create a function which is used to update the label every time the timer fires and a function which schedules the timer.
Obj-c///
- (void)timerFired {
self.seconds += 0.1f;
self.label.text = [NSString stringWithFormat:#"%f", self.seconds];
if (self.seconds >= limit) {
[self.updateTimer invalidate];
}
}
Swift///
func timerFired() {
seconds += 0.1 //Increment the seconds
label.text = "\(seconds)" //Set the label
if (seconds >= limit) {
timer.invalidate()
}
}

UIButton won't generate randomly when score is added?

I am having a problem with my coding on Xcode. I am making an app in which a target appears, then you have to tap it, but the time allowed between each tap shortens gradually.
MY PROBLEM: whenever I try and make a UILabel update after a tap, the target goes back to it's original position on my view controller. I have all relevant code here:
-(IBAction)startButtonPressed:(id)sender {
startButton.hidden = YES;
Text.hidden = YES;
targX = arc4random() %769;
targY = arc4random() % 925;
Target.center = CGPointMake(targX, targY);
Target.hidden = NO;
}
-(IBAction)targetTapped:(id)sender {
[Time invalidate];
targX = arc4random() %769;
targY = arc4random() % 925;
[self scoreAdd:(id)sender];
Target.center = CGPointMake(targX, targY);
timeMax = 5 - (Score * 0.05);
if (Score >= 90) {
timeMax = 0.5;
}
Time = [NSTimer scheduledTimerWithTimeInterval:timeMax target:self selector:#selector(Lose:) userInfo:nil repeats:NO];
}
-(void)Lose:(id)sender {
Target.hidden = YES;
Text.hidden = NO;
}
-(void)scoreAdd:(id)sender{
Score = Score + 1;
scoreLabel.text = [NSString stringWithFormat:#"Score: %li", Score];
}
print
targX = arc4random() %769;
targY = arc4random() % 925;
in both methods , check whether you are getting proper points. And check generated points lies with in device screen.

working with UIProgressView to set progress bar to 30 minutes

I'm working on an project were I have put in a progress bar and then set a button to start the progressbar when it is pressed. The thing is that I want to have the progress in 30min when you pressed the startbutton, so I need help to set it to 30 minutes.
Here is my code to setup the progressbar and startbutton
the m-file
- (IBAction)StartButton:(id)sender {
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(moreProgress)
userInfo:Nil repeats:YES];
[super viewDidLoad];
//progressbar start when button pressed
}
and then i got this for the time
//selector progress time
- (void) moreProgress {
myProgressView.progress += 0.001;
}
I think it's only a math problem.
First, myProgressView.progress can assume values from 0.0 to 1.0.
As you call moreProgress each second, you'll need to call it 1800 times for 30 minutes. Thus, myProgressView.progress should increase 1/1800 by second.
Change your code to:
- (void) moreProgress {
myProgressView.progress += 1.0/1800.0;
}
Hope it helps you! :)
1)Save the Current Time (You can save it to NSUserDefaults by example).
You can do something like That:
NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
NSDate *currentDate = [NSDate date];
[user1 setObject:[NSNumber numberWithDouble:[currentDate timeIntervalSince1970]] forKey:#"time"];
[user1 synchronize];
2)schedule the call to your function like you did it
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(moreProgress)
userInfo:Nil repeats:YES];
3)Next in your function you need to update the progress view status with something like that:
//selector progress time
- (void) moreProgress {
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSDate *startTime = [NSDate dateWithTimeIntervalSince1970:[[user objectForKey:#"time"]doubleValue]];
NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow];
float progress = (float)elapsed time / 30.0 * 60.0;
myProgressView.progress = progress;
if ((float)elapsedTime / 30 * 60 >= 1)
{
// The timer is end, you should stop it and do what you need
}
}
Note:
It s not safe to expect that
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(moreProgress)
will fire up each seconds. It s not guaranteed and not precise.
// This extension will help you. I'had used this extension and it really work for me.
extension UIProgressView {
#available(iOS 10.0, *)
func setAnimatedProgress(progress: Float = 1, duration: Float = 1, completion: (() -> ())? = nil) {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
DispatchQueue.main.async {
let current = self.progress
self.setProgress(current+(1/duration), animated: true)
}
if self.progress >= progress {
timer.invalidate()
if completion != nil {
completion!()
}
}
}
}
}
// Just call this extension with your UIProgressView like this
YOUR_PROGRESS_VIEW.setAnimatedProgress(duration: 100) { // Here 100 meaning 10 seconds
debugPrint("Done!")
}

NSTimer Movement irregular

I am trying to make a game movement animation in objective c so that when a button is pressed something will move across the screen while it is pressed. This works fine though the calling of the NSTimer which I am using is irregular and I am getting laggy movements. This is in iOS 6 so I wont use sprite kit as it is not available
Any suggestions on how to make it a regular call or to make it a smooth animation would be much appreciated
Thanks in advance
Here is the code:
-(void) click {
if (!self.animating) {
self.animating = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:#selector(move)
userInfo:nil
repeats:YES];
}
}
- (void) cancelClick {
self.animating = NO;
[timer invalidate];
}
-(void) move {
CGPoint origin = self.target.frame.origin;
if ([self.direction isEqual:#"left"]) {
origin.x -= self.magnitude;
} else if ([self.direction isEqual:#"right"]) {
origin.x += _magnitude;
} else if ([self.direction isEqual:#"up"]) {
origin.y -= _magnitude;
} else {
origin.y += _magnitude;
}
[self.target move:0 toPoint:origin animated:YES delay:0 animationOptions:UIViewAnimationOptionCurveLinear];
}

UIView Get Position during Animatoin

I have looked at every instance of this question on Stack as well as various other websites, its still not working out for me. I am trying to get the position of my object while the animation is in session.
Here is what I have:
- (void)viewDidLoad{
currentPosition = [[bikeMotion.layer presentationLayer] center]; //its a CGPoint
}
- (void)doAnimation {
bikeMotion = [[DotAnimation alloc]initWithFrame:CGRectMake((*bikePathPoints)[0].x, (*bikePathPoints)[0].y, 8, 8)];
//... [self animate stuff]...
[NSTimer scheduledTimerWithTimeInterval:0.07f target:self selector:#selector(backgroundAnimations) userInfo:nil repeats:YES];
}
-(void)backgroundAnimations{
cout << currentPosition.x << endl;
}
I also tried this:
- (void)viewDidLoad{
currentPosition = [[bikeMotion.layer presentationLayer] frame]; //its a CGRect
}
- (void)doAnimation {
bikeMotion = [[DotAnimation alloc]initWithFrame:CGRectMake((*bikePathPoints)[0].x, (*bikePathPoints)[0].y, 8, 8)];
//...[self animation stuff];
[NSTimer scheduledTimerWithTimeInterval:0.07f target:self selector:#selector(backgroundAnimations) userInfo:nil repeats:YES];
}
-(void)backgroundAnimations{
cout << currentPosition.origin.x << endl;
}
I have even tried this:
- (void)viewDidLoad{
p = (CALayer*)[bikeMotion.layer presentationLayer];
}
-(void)doAnimation{
bikeMotion = [[DotAnimation alloc]initWithFrame:CGRectMake((*bikePathPoints)[0].x, (*bikePathPoints)[0].y, 8, 8)];
[NSTimer scheduledTimerWithTimeInterval:0.07f target:self selector:#selector(backgroundAnimations) userInfo:nil repeats:YES];
}
-(void)backgroundAnimations{
CGPoint position = [p position];
cout << position.x << endl;
}
Have you tried object.layer.anchorpoint? You'll need to import QuartzCore.

Resources