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];
Related
I'm making a simple little game.
I have two ints:
1) One to make an object's position to be generated randomly on the Y- and X-axis.
2) One for the scoring function.
I use NSTimers to make the objects move, and for the score to add 1 every second.
The problem is that the game gets all bugged and the timers start going crazy when the both ints exist. If I remove the scoring int/timer, the objects move perfectly. And the other way around.
I can't seem to find the issue since it should be working.
Any ideas?
ViewController.h:
int scoreNumber;
int randomPosition;
ViewController.m:
#define IsIphone4 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON )
#define IsIphone5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IsIphone6 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )667 ) < DBL_EPSILON )
#define IsIphone6Plus ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )736 ) < DBL_EPSILON )
- (void)obstaclesMoving {
obstacle.center = CGPointMake(obstacle.center.x - 1, obstacle.center.y);
obstacle2.center = CGPointMake(obstacle2.center.x - 1, obstacle2.center.y);
obstacle3.center = CGPointMake(obstacle3.center.x - 1, obstacle3.center.y);
if (IsIphone4) {
if (obstacle.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 70;
obstacle.center = CGPointMake(320, randomPosition);
}
if (obstacle2.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 360;
obstacle2.center = CGPointMake(320, randomPosition);
}
if (obstacle3.center.x < 0) {
randomPosition = arc4random_uniform(100);
randomPosition = randomPosition + 215;
obstacle3.center = CGPointMake(320, randomPosition);
}
}
if (IsIphone5) {
if (obstacle.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 70;
obstacle.center = CGPointMake(320, randomPosition);
}
if (obstacle2.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 448;
obstacle2.center = CGPointMake(320, randomPosition);
}
if (obstacle3.center.x < 0) {
randomPosition = arc4random_uniform(100);
randomPosition = randomPosition + 259;
obstacle3.center = CGPointMake(320, randomPosition);
}
}
if (IsIphone6) {
if (obstacle.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 70;
obstacle.center = CGPointMake(375, randomPosition);
}
if (obstacle2.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 547;
obstacle2.center = CGPointMake(375, randomPosition);
}
if (obstacle3.center.x < 0) {
randomPosition = arc4random_uniform(100);
randomPosition = randomPosition + 309;
obstacle3.center = CGPointMake(375, randomPosition);
}
}
if (IsIphone6Plus) {
if (obstacle.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 70;
obstacle.center = CGPointMake(414, randomPosition);
}
if (obstacle2.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 616;
obstacle2.center = CGPointMake(414, randomPosition);
}
if (obstacle3.center.x < 0) {
randomPosition = arc4random_uniform(100);
randomPosition = randomPosition + 343;
obstacle3.center = CGPointMake(414, randomPosition);
}
}
}
- (IBAction)characterUp:(id)sender {
characterDrop = 5;
}
- (IBAction)characterDown:(id)sender {
characterDrop = -5;
}
- (void)characterMoving {
character.center = CGPointMake(character.center.x, character.center.y - characterDrop);
characterDrop = characterDrop - 0.1;
if ((CGRectIntersectsRect(character.frame, ground.frame)) && (characterDrop < -1)) {
[self gameOver];
//Sound for G.O.
}
if ((CGRectIntersectsRect(character.frame, roof.frame)) && (characterDrop < -1)) {
[self gameOver];
//Sound for G.O.
}
}
- (IBAction)startGame:(id)sender {
startGameButton.hidden = YES;
characterMovement = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(characterMoving) userInfo:nil repeats:YES];
characterDrop = -5;
obstacleTimer = [NSTimer scheduledTimerWithTimeInterval:0.0055 target:self selector:#selector(obstaclesMoving) userInfo:nil repeats:YES];
scorer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(scoring) userInfo:nil repeats:YES];
}
- (void)scoring {
scoreNumber = scoreNumber + 1;
scoreLabelInGame.text = [NSString stringWithFormat:#"%i", scoreNumber];
scoreLabelGameOver.text = [NSString stringWithFormat:#"%i", scoreNumber];
}
- (void)gameOver {
if (scoreNumber > bestScoreNumber) {
[[NSUserDefaults standardUserDefaults] setInteger:scoreNumber forKey:#"bestScoreSaved"];
bestScoreLabel.text = [NSString stringWithFormat:#"New best: %i", scoreNumber];
}
character.hidden = YES;
obstacle.hidden = YES;
obstacle2.hidden = YES;
obstacle3.hidden = YES;
startGameButton.hidden = YES;
scoreLabelInGame.hidden = YES;
characterUpButton.hidden = YES;
characterDownButton.hidden = YES;
scoreLabelGameOver.hidden = NO;
bestScoreLabel.hidden = NO;
gameOverLabel.hidden = NO;
restartGameButton.hidden = NO;
backButton.hidden = NO;
[characterMovement invalidate];
[obstacleTimer invalidate];
[obstacleTimer2 invalidate];
[obstacleTimer3 invalidate];
[obstacleTimer4 invalidate];
[scorer invalidate];
}
- (void)viewDidLoad {
bestScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"bestScoreSaved"];
bestScoreLabel.text = [NSString stringWithFormat:#"Best: %li", (long)bestScoreNumber];
scoreNumber = 0;
scoreLabelGameOver.hidden = YES;
bestScoreLabel.hidden = YES;
gameOverLabel.hidden = YES;
restartGameButton.hidden = YES;
backButton.hidden = YES;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
I think a cleaner approach would be to move to a CADisplayLink or a single NSTimer and move your views off of the selector it calls.
/* Add _amountOfTicks as an instance Variable */
{
int _amountOfTicks;
int scoreNumber;
int randomPosition;
}
/* replace timers in startGame */
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:#selector(tick)];
displayLink.frameInterval = 1;
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
- (void)tick {
_amountOfTicks++;
if (_amountOfTicks > 16){
_amountOfTicks = 1;
}
if (_amountOfTicks == 2) {
[self characterMoving]
}
if (_amountOfTicks == 4) {
[self obstaclesMoving]
}
if (_amountOfTicks == 16) {
[self scoring]
}
}
Also, in the method below you're reducing the center.x by 1 then changing it back to the screens width directly below when checking for macros.
- (void)obstaclesMoving {
obstacle.center = CGPointMake(obstacle.center.x - 1, obstacle.center.y); /* reduce by 1 */
obstacle2.center = CGPointMake(obstacle2.center.x - 1, obstacle2.center.y);
obstacle3.center = CGPointMake(obstacle3.center.x - 1, obstacle3.center.y);
if (IsIphone4) {
if (obstacle.center.x < 0) {
randomPosition = arc4random_uniform(60);
randomPosition = randomPosition + 70;
obstacle.center = CGPointMake(320, randomPosition); /* changes it back to screen width which makes what you did up above useless */
}
}
From the docs:
the effective resolution of the time interval for a timer is limited
to on the order of 50-100 milliseconds
My guess is that this maybe the issue.
If you need to your score increment every 1 sec, why you calling that function every 0.0055 sec?
You also need to move [super viewDidLoad]; to the top of you viewDidLoad function.
Also as #rmaddy stated, you shouldn't be explicitly checking for the screen size.
I can tell you that running a timer with a resolution of 1/180th of a second is going to get you into trouble. Since the frame is updated at most every 60th of a second, the user isn't going to see it. There is no guarantee that the timers will actually run that often (most likely they won't).
You can't use timers like that. You can only use them to trigger an event, and then you need to check exactly what time it is and what you should do.
What might be happening is that you managed to turn autolayout on.
I have a pong game with computer and player working well was looking if anyone could help in making a team selection view controller so players can choose their side. Also looking to control the ball speed from a settings controller. If any one could help would be awesome thank you.
My code I'm using is :
#implementation Game
-(void)Collision{
if (CGRectIntersectsRect(Ball.frame, Player.frame)) {
Y = arc4random() %5;
Y = 0-Y;
}
if (CGRectIntersectsRect(Ball.frame, Computer.frame)) {
Y = arc4random() %5;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Drag = [[event allTouches] anyObject];
Player.center = [Drag locationInView:self.view];
if (Player.center.y > 620) {
Player.center = CGPointMake(Player.center.x, 620);
}
if (Player.center.y < 620) {
Player.center = CGPointMake(Player.center.x, 620);
}
if (Player.center.x < 25) {
Player.center = CGPointMake(25, Player.center.y);
}
if (Player.center.x > 340) {
Player.center = CGPointMake(340, Player.center.y);
}
}
-(void)ComputerMovement{
if (Computer.center.x > Ball.center.x) {
Computer.center = CGPointMake(Computer.center.x - 2, Computer.center.y);
}
if (Computer.center.x < Ball.center.x) {
Computer.center = CGPointMake(Computer.center.x + 2, Computer.center.y);
}
if (Computer.center.x < 25) {
Computer.center = CGPointMake(25, Computer.center.y);
}
if (Computer.center.x > 340) {
Computer.center = CGPointMake(340, Computer.center.y);
}
}
-(IBAction)StartButton:(id)sender{
StartButton.hidden = YES;
Exit.hidden = YES;
WinOrLose.hidden = YES;
Y = arc4random() %11;
Y = Y- 5;
X = arc4random() %11;
X = X - 5;
if (Y == 0){
Y =1;
}
if (X==0){
X = 1;
}
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(BallMovement) userInfo:nil repeats:YES];
}
}
-(void)BallMovement{
[self ComputerMovement];
[self Collision];
Ball.center= CGPointMake(Ball.center.x + X, Ball.center.y + Y);
if (Ball.center.x <5) {
X = 0 - X;
}
if (Ball.center.x >340) {
X = 0 - X;
}
if (Ball.center.y < 0) {
PlayerScoreNumber = PlayerScoreNumber + 1;
PlayerScore.text = [ NSString stringWithFormat:#"%i", PlayerScoreNumber];
[timer invalidate];
StartButton.hidden = NO;
WinOrLose.hidden = YES;
Ball.center = CGPointMake(147, 326);
Computer.center = CGPointMake(134, 28);
if (PlayerScoreNumber == 10) {
StartButton.hidden = YES;
Exit.hidden = NO;
WinOrLose. Hidden = NO;
WinOrLose.text = [NSString stringWithFormat:#"You Win!"];
}
}
if (Ball.center.y > 640) {
ComputerScoreNumber = ComputerScoreNumber + 1;
ComputerScore.text = [NSString stringWithFormat:#"%i", ComputerScoreNumber];
[timer invalidate];
StartButton.hidden = NO;
Ball.center = CGPointMake(147, 3269);
Computer.center = CGPointMake(134, 28);
if (ComputerScoreNumber == 10) {
StartButton.hidden =YES;
Exit.hidden = NO;
WinOrLose. Hidden = NO;
WinOrLose.text = [NSString stringWithFormat:#"You Lose!"];
}
}
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
I would start with a Storyboard to create the needed ViewControllers. After that you can connect them with segues and model the transition triggers.
I've written a small tutorial about this some time ago. Maybe this helps:
HowTo: Add View Controllers to the game storyboard and use segues to navigate between them
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];
}
At the time of this post, I am using the latest versions of XCode and SpriteBuilder.
I have 2 games, one of which is built using purely Objective-C on XCode and another which uses Objective-C on XCode as well as SpriteBuilder. The non-SpriteBuilder game has a tap to play screen whose background looks the same as the in-game background. The second you tap the screen, you go straight into the game.
The Sprite Builder game is the one I am trying to implement a Start screen on. I read a few tutorials, http://www.reigndesign.com/blog/creating-a-simple-menu-with-scene-transition-in-cocos2d/, but it didn't help me. I don't want to transition from one screen to another using different background images.
This is the start screen that my non-SpriteBuilder game uses:
-(void)NewGame{
Bottom1.hidden = YES;
Bottom2.hidden = YES;
Bottom3.hidden = YES;
Bottom4.hidden = YES;
Bottom5.hidden = YES;
Bottom6.hidden = YES;
Bottom7.hidden = YES;
Top1.hidden = YES;
Top2.hidden = YES;
Top3.hidden = YES;
Top4.hidden = YES;
Top5.hidden = YES;
Top6.hidden = YES;
Top7.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;
Heli.hidden = NO;
Heli.center = CGPointMake(31, 74);
Heli.image = [UIImage imageNamed:#"HeliUp.png"];
Start = YES;
ScoreNumber = 0;
Score.text = [NSString stringWithFormat:#"Score: 0"];
Intro3.text = [NSString stringWithFormat:#"High Score: %i", HighScore];
All obstacles are hidden except the background and the intro labels. You also see your high score and the tap to play button.
When you tap the screen, this is the next code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (Start == YES) {
Intro1.hidden = YES;
Intro2.hidden = YES;
Intro3.hidden = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(HeliMove) userInfo:nil repeats:YES];
Scorer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(Scoring) userInfo:nil repeats:YES];
Start = NO;
Bottom1.hidden = NO;
Bottom2.hidden = NO;
Bottom3.hidden = NO;
Bottom4.hidden = NO;
Bottom5.hidden = NO;
Bottom6.hidden = NO;
Bottom7.hidden = NO;
Top1.hidden = NO;
Top2.hidden = NO;
Top3.hidden = NO;
Top4.hidden = NO;
Top5.hidden = NO;
Top6.hidden = NO;
Top7.hidden = NO;
Obstacle.hidden = NO;
Obstacle2.hidden = NO;
RandomPosition = arc4random() %75;
RandomPosition = RandomPosition + 110;
Obstacle.center = CGPointMake(570,RandomPosition);
RandomPosition = arc4random() %75;
RandomPosition = RandomPosition + 110;
Obstacle2.center = CGPointMake(855,RandomPosition);
RandomPosition = arc4random() %55;
Top1.center = CGPointMake(560, RandomPosition);
RandomPosition = RandomPosition +265;
Bottom1.center = CGPointMake(560, RandomPosition);
RandomPosition = arc4random() %55;
Top2.center = CGPointMake(640, RandomPosition);
RandomPosition = RandomPosition +265;
Bottom2.center = CGPointMake(640, RandomPosition);
RandomPosition = arc4random() %55;
Top3.center = CGPointMake(720, RandomPosition);
RandomPosition = RandomPosition +265;
Bottom3.center = CGPointMake(720, RandomPosition);
RandomPosition = arc4random() %55;
Top4.center = CGPointMake(800, RandomPosition);
RandomPosition = RandomPosition +265;
Bottom4.center = CGPointMake(800, RandomPosition);
RandomPosition = arc4random() %55;
Top5.center = CGPointMake(880, RandomPosition);
RandomPosition = RandomPosition +265;
Bottom5.center = CGPointMake(880, RandomPosition);
RandomPosition = arc4random() %55;
Top6.center = CGPointMake(960, RandomPosition);
RandomPosition = RandomPosition +265;
Bottom6.center = CGPointMake(960, RandomPosition);
RandomPosition = arc4random() %55;
Top7.center = CGPointMake(1040, RandomPosition);
RandomPosition = RandomPosition +265;
Bottom7.center = CGPointMake(1040, RandomPosition);
}
Y = -7;
Heli.image = [UIImage imageNamed:#"HeliUp.png"];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
Y = 7;
Heli.image = [UIImage imageNamed:#"HeliDown.png"];
}
In basic terms, all obstacles are loaded and are generated in random positions, the object moves with u tap the screen and drops if you don't tap the screen. The intro labels are hidden.
Here's the code for my SpriteBuilder game:
- (void)didLoadFromCCB {
self.userInteractionEnabled = TRUE;
_grounds = #[_ground1, _ground2];
for (CCNode *ground in _grounds) {
// set collision txpe
ground.physicsBody.collisionType = #"level";
ground.zOrder = DrawingOrderGround;
_scrollSpeed = 80.f;
}
// set this class as delegate
_physicsNode.collisionDelegate = self;
// set collision txpe
_hero.physicsBody.collisionType = #"hero";
_hero.zOrder = DrawingOrdeHero;
_obstacles = [NSMutableArray array];
[self spawnNewObstacle];
[self spawnNewObstacle];
[self spawnNewObstacle];
}
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (!_gameOver) {
[_hero.physicsBody applyImpulse:ccp(0, 400.f)];
[_hero.physicsBody applyAngularImpulse:10000.f];
_sinceTouch = 0.f;
}
}
Really short code. Basically, after the logo screen disappears, the game starts right away without a start screen. When the game ends, it restarts and you immediately start playing again.
Trying to figure out how I can add another void statement to the SpriteBuilder game so that a person goes from tapping the screen then to playing the game instead of immediately going into playing the game.
I can add the viewDidLoad statement to the SpriteBuilder game but I wonder if it will conflict with the didLoadfromCCB statement. I need to have the start screen transition into the game screen using the same background. Only difference is, the start screen will hide the obstacles and show the play now labels, then the labels will hide and obstacles become visible in the game screen.
I would suggest doing it following the spritebuilder tutorial. This will probably require you to rejig your code. The following tutorial also helps you with linking spritebuilder to backend code, and also how to transition between scenes.
I'm not 100% sure why you included so much detail in your question, I'm confused with what is game logic, and what is other logic... So If I'm off the mark, let me know and I'll update my answer.
https://www.makegameswith.us/tutorials/getting-started-with-spritebuilder/menus/
Smells a bit like Flappy Bird to me.
Either way I would implement it with a gameState variable and a layer with the menu in.
When you open the game (viewDidLoad) set the gameState to 0 e.g. Menu
then in the touch method I would have a switch like the following:
switch(gameState){
case 0: //Menu
menuLayer.hidden = YES;
gameState = 1 //IN_GAME
break;
case 1: //In game
//Do your game tap code
break;
case 2: //Game Over
gameOverLayer.hidden = NO;
break;
}
Or you could duplicate the CCb and remove the game code and load the game cCB on touch, but I think the former is the best option.
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.