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.
Related
I am getting the error : CGAffineTransformInvert: singular matrix error every time i run my app but i do not know what is causing the problem. The code is below could someone help me please. Anyone who answers i thank in advance.
#import "Squirrel.h"
#interface Squirrel ()
#end
#implementation Squirrel
-(void)Scoring{
ScoreNumber = ScoreNumber + AddedScore;
AddedScore = AddedScore - 1;
if (AddedScore < 0){
AddedScore = 0;
}
Score.text=[NSString stringWithFormat:#"Score: %i", ScoreNumber];
}
-(void)Gameover{
Acorn1.hidden=YES;
Branch1.hidden=YES;
}
-(void)Losing{
if(CGRectContainsRect(Sammy.frame, Acorn1.frame)){
AddedScore=1;
RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Acorn1.center=CGPointMake(RandomPosition, 88);
}
if(CGRectContainsRect(Sammy.frame, Acorn2.frame)){
AddedScore=1;
/*RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Acorn2.center=CGPointMake(RandomPosition, 56);*/
}
if(CGRectContainsRect(Sammy.frame, Acorn3.frame)){
AddedScore=1;
}
if(CGRectContainsRect(Sammy.frame, Branch1.frame)){
//[self Gameover];
AddedScore=1;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
SquirrelLeft=NO;
SquirrelRight=NO;
StopSideMovement=YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
if(point.x < 160){
SquirrelLeft=YES;
}
else{
SquirrelRight=YES;
}
}
-(void)moving{
Sammy.center = CGPointMake(Sammy.center.x + SideMovement, 366);
if(Acorn1.center.y > 463){
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
Acorn1.center = CGPointMake(RandomPosition, 16);
}
if(Acorn2.center.y > 463){
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
Acorn2.center = CGPointMake(RandomPosition, 16);
}
if(Acorn3.center.y > 463){
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
Acorn3.center = CGPointMake(RandomPosition, 16);
}
if(Branch1.center.y > 463){
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
Branch1.center = CGPointMake(RandomPosition, 16);
}
if(Branch2.center.y > 463){
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
Branch2.center = CGPointMake(RandomPosition, 16);
}
if(Branch3.center.y > 463){
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
Branch3.center = CGPointMake(RandomPosition, 16);
}
if(Branch4.center.y > 463){
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
Branch4.center = CGPointMake(RandomPosition, 16);
}
if(SquirrelLeft == YES){
SideMovement = SideMovement-0.8;
if(SideMovement < -5){
SideMovement = -5;
}
}
if(SquirrelRight ==YES){
SideMovement = SideMovement + 0.8;
if(SideMovement > 5){
SideMovement = 5;
}
}
if(StopSideMovement == YES && SideMovement>0){
SideMovement = SideMovement - 0.1;
if (SideMovement < 0){
SideMovement = 0;
StopSideMovement=NO;
}
}
if (StopSideMovement == YES && SideMovement<0){
SideMovement=SideMovement+0.1;
if(SideMovement>0){
SideMovement=0;
StopSideMovement=NO;
}
}
if(AddedScore == 1 && CGRectContainsRect(Sammy.frame, Acorn1.frame)){
Branch1.center = CGPointMake(Branch1.center.x, Branch1.center.y + 6);
Branch2.center = CGPointMake(Branch2.center.x, Branch2.center.y + 6);
Branch3.center = CGPointMake(Branch3.center.x, Branch3.center.y + 6);
Branch4.center = CGPointMake(Branch4.center.x, Branch4.center.y + 6);
Acorn2.center = CGPointMake(Acorn2.center.x, Acorn2.center.y + 5);
Acorn3.center = CGPointMake(Acorn3.center.x, Acorn3.center.y + 5);
}
}
-(void)moveBranch1{
Branch1.center = CGPointMake(Branch1.center.x, Branch1.center.y + 6);
}
-(void)moveBranch2{
Branch2.center = CGPointMake(Branch2.center.x, Branch2.center.y + 6);
}
-(void)moveBranch3{
Branch3.center = CGPointMake(Branch3.center.x, Branch3.center.y + 6);
}
-(void)moveBranch4{
Branch4.center = CGPointMake(Branch4.center.x, Branch4.center.y + 6);
}
-(void)moveAcorn1{
Acorn1.center = CGPointMake(Acorn1.center.x, Acorn1.center.y + 5);
}
-(void)moveAcorn2{
Acorn2.center = CGPointMake(Acorn2.center.x, Acorn2.center.y + 5);
}
-(void)moveAcorn3{
Acorn3.center = CGPointMake(Acorn3.center.x, Acorn3.center.y + 5);
}
- (IBAction)Start:(id)sender {
Acorn1.hidden=NO;
Acorn2.hidden=NO;
Acorn3.hidden=NO;
Branch1.hidden=NO;
Branch2.hidden=NO;
Branch3.hidden=NO;
Branch4.hidden=NO;
Title.hidden=YES;
Mu.hidden=YES;
Main.hidden=YES;
Start.hidden=YES;
HighScore.hidden=YES;
How.hidden=YES;
MoveAcorn1 = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(moveAcorn1) userInfo:nil repeats:YES];
MoveAcorn2 = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(moveAcorn2) userInfo:nil repeats:YES];
MoveAcorn3 = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(moveAcorn3) userInfo:nil repeats:YES];
MoveBranch1 = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(moveBranch1) userInfo:nil repeats:YES];
MoveBranch2 = [NSTimer scheduledTimerWithTimeInterval:0.09 target:self selector:#selector(moveBranch2) userInfo:nil repeats:YES];
MoveBranch3 = [NSTimer scheduledTimerWithTimeInterval:0.09 target:self selector:#selector(moveBranch3) userInfo:nil repeats:YES];
MoveBranch4 = [NSTimer scheduledTimerWithTimeInterval:0.09 target:self selector:#selector(moveBranch4) userInfo:nil repeats:YES];
Moving = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(moving) userInfo:nil repeats:YES];
losing = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(Losing) userInfo:nil repeats:YES];
Scoring = [NSTimer scheduledTimerWithTimeInterval:0.00000001 target:self selector:#selector(Scoring) userInfo:nil repeats:YES];
RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Acorn1.center=CGPointMake(RandomPosition, 88);
RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Acorn2.center=CGPointMake(RandomPosition, 56);
RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Acorn3.center=CGPointMake(RandomPosition, 22);
RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Branch1.center=CGPointMake(RandomPosition, 35);
RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Branch2.center=CGPointMake(RandomPosition, 25);
RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Branch3.center=CGPointMake(RandomPosition, 77);
RandomPosition=arc4random()%248;
RandomPosition=RandomPosition+36;
Branch4.center=CGPointMake(RandomPosition, 43);
}
- (void)viewDidLoad {
Acorn1.hidden=YES;
Acorn2.hidden=YES;
Acorn3.hidden=YES;
Branch1.hidden=YES;
Branch2.hidden=YES;
Branch3.hidden=YES;
Branch4.hidden=YES;
[super viewDidLoad];
When I try to release a path it will cause a memory leak that is about 5 megabytes big. Every time i call CGreleasePath the ram memory usage will increase again by 5 megabytes. What could be the reason that is causing this memory leak?
This is the code where all my paths are getting used:
//viewcontroller.h
CGMutablePathRef path;
//viewcontroller.m
-(void)viewDidLoad{
path = CGPathCreateMutable;
timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(movement1) userInfo:nil repeats:YES];
timer2 = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(motor) userInfo:nil repeats:YES];
}
-(void)movement1{
CGPathMoveToPoint(path, NULL, greentmporary.x, greentmporary.y);
CGPathAddLineToPoint(path, NULL, greenpoint1.x, greenpoint1.y);
}
-(void)motor{
CGPathRef thickpath = NULL;
thickpath = CGPathCreateCopyByStrokingPath(path, NULL, (6+bigsizeYellow), kCGLineCapSquare, kCGLineJoinMiter, 0);
if (CGPathContainsPoint(thickpath, NULL, greenpoint1, NO) && krockamedgreen == NO) {
NSLog(#"YES");
[self reset]
}
if (CGPathContainsPoint(thickpath, NULL, point1, NO) && krockamedgreen == NO) {
NSLog(#"YES");
[self reset]
}
if (thickpath) {
CFRelease(thickpath);
thickpath = NULL;
}
}
-(void)reset{
path = NULL;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//start again
{
path = CGPathCreateMutable;
}
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];
}
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];
}
I'm new to objective c, I have created an app in which the bees will be dropping and a bear will try to catch them, everything is working fine. Now i want to limit of the number bees dropping to 20 or something, could anyone help me how to do that?
I'm not sure whether to put the timer in loop or the drop bee method in loop, any input in this will be highly helpful.
beeArray = [[NSMutableArray alloc]init];
timer1 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(dropBee) userInfo:nil repeats:YES];
-(void)dropBee
{
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
// CGFloat screenHeight = screenRect.size.height;
UIImageView *beeImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"bee-honey.png"]];
int resultX = (int)roundf(screenWidth);
int randomX = 0 + rand()%(resultX -0);
[beeArray addObject:beeImage];
beeImage.frame = CGRectMake(50, -10, 50, 50);
beeImage.center = CGPointMake(randomX, 50);
[self.view addSubview:beeImage];
[UIView beginAnimations:#"beemovement" context:NULL];
[UIView setAnimationDuration:2];
beeImage.center = CGPointMake(randomX, 600);
[UIView commitAnimations];
}
How are you adding bees to your application? It would be useful to see some code. If you have a method which is something like -(void)addABee then you could create a variable to count the number of bees.
In your header file:
#interface SomeController : UIViewController {
int _maxNumberOfBees;
}
Then in your implementation:
-(void)theMethodYouUseToCreateYourBeeArray {
beeArray = [[NSMutableArray alloc]init];
timer1 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(dropBee) userInfo:nil repeats:YES];
_maxNumberOfBees = 20;
// etc...
}
-(void)dropBee {
if ([beeArray count] >= _maxNumberOfBees) {
// if the number of bees is greater than or equal to _maxNumberOfBees...
NSLog(#"Too many bees!");
} else {
// add your bee to the scene
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
// etc etc etc...
}
}
Hope that helps.