Assigning an object velocity?

Discussion in 'iOS Programming' started by joeysefika, Sep 27, 2009.

  1. joeysefika macrumors newbie

    Joined:
    Sep 26, 2009
    #1
    Alrighty so a very helpful guy helped me out with randomly generating an object but at the moment its just sitting still, i need it to move down the screen on the Y axis upon spawn of the object.
    Code:
    #define kSpeedY 7
    #define kSpeedX 0
    
    
    #define kScoreToWin 10
    
    @implementation Space_AdventureViewController
    @synthesize player,player_score,computer_score,gameState,tapToBegin;
    @synthesize velocity;
    
    /*
     // The designated initializer. Override to perform setup that is required before the view is loaded.
     - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
     // Custom initialization
     }
     return self;
     }
     */
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	if(gameState == kGameStatePaused) {
    		tapToBegin.hidden = YES;
    		gameState = kGameStateRunning;
    	} else if(gameState == kGameStateRunning) {
    		[self touchesMoved:touches withEvent:event];
    	}
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	UITouch *touch = [[event allTouches] anyObject];
    	CGPoint location = [touch locationInView:touch.view];
    	CGPoint xLocation = CGPointMake(location.x,player.center.y);
    	player.center = xLocation;
    }
    
    -(void) gameLoop {
    	
    
    }
    
    -(void)reset:(BOOL) newGame {
    	self.gameState = kGameStatePaused;
    	SpaceJunk.center = self.view.center;
    	if(newGame) {
    		if(computer_score_value > player_score_value) {
    			tapToBegin.text = @"You Died, The Earth Is Doomed";
    		} else {
    			tapToBegin.text = @"You Survived the Onslaught";
    		}
    		
    		computer_score_value = 0;
    		player_score_value = 0;
    	} else {
    		tapToBegin.text = @"Tap To Begin";
    	}
    	
    	player_score.text = [NSString stringWithFormat:@"%d",player_score_value];
    	computer_score.text = [NSString stringWithFormat:@"%d",computer_score_value];
    }
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    	self.gameState = kGameStatePaused;
    	[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
    	velocity = CGPointMake(kSpeedX, kSpeedX);
    	
    	//HERE IS THE SPRITES ANIMATION AND SPAWN INFO
    	//*
    	//*
    	int randomX = arc4random() % 320 + 1;
    	SpaceJunkImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"flash1.png"], [UIImage imageNamed:@"flash3.png"], [UIImage imageNamed:@"flash4.png"], [UIImage imageNamed:@"flash5.png"], [UIImage imageNamed:@"flash2.png"], nil];
    	
    	SpaceJunk = [[UIImageView alloc] initWithFrame: CGRectMake (randomX, 20, 30, 30)];
    	SpaceJunk.animationDuration = 0.5;
    	SpaceJunk.contentMode = UIViewContentModeBottomLeft;
    	SpaceJunk.animationImages = SpaceJunkImages;
    	[SpaceJunk startAnimating];
    	
    	[self.view addSubview:SpaceJunk];
    	//*
    	//*
    }
    
    /*
     // Override to allow orientations other than the default portrait orientation.
     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     // Return YES for supported orientations
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
     }
     */
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
        // Release anything that's not essential, such as cached data
    }
    
    - (void)dealloc {
        [super dealloc];
    	[player release];
    	[player_score release];
    	[computer_score release];
    	[tapToBegin release];
    }
    
    Thanks for your help:)
     
  2. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #2
    one way to do it is to set a start and end point and then start animating (i don't know if this will work i never use the built in animation part)

    another way is to do something similar to openGL where you have a draw method called like 60 times, and each time update its position by moving the y axis down a little bit
    one way maybe to do it is if you have (and only works if only the moving objects are drawn last)
    for ( Subviews in view.subview)
    {
    if (Subviews > 5) (for example 5 other subvies you don't want to move before it, but anything after the 5 subviews you want to move down the screen)
    // code to just move the subview down or to create a new rectangle but have the y down a bit

    hard to do as i'm not on my mac and code is more pseudocode rather than code that will work, but may just give you an ide ain what to do

    btw everything is a subview, any button placed is consider a subview (one for each button or textfield etc)
     
  3. joeysefika thread starter macrumors newbie

    Joined:
    Sep 26, 2009
    #3
    Hmm, is there a way to simply go something like
    Code:
    SpaceJunk = velocity(kSpeedx, kSpeedy);
    ?
     
  4. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #4
    you could but then you need a method where you use that velocity to move the unit ( don't know if there is one built into Quartz2D or Coca but im going to guess not)
    you will have to update the position on the image and moveit down slightly, i would try a few different ways and see whats kinda of working maybe then post code samples for others to see what you doing and give feedback or help with bugs
     

Share This Page