I try to making a splash screen
all i want is wait at splash screen for 1 sec then automatically load to play screen .
I try to use Thread.Sleep() but it's not work .
Any idea,Plsssssssss .
THANKS YOU :D
try something like this
timer = 10000f; // 10 seconds
Then in the update method:
timer -= gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer <= 0.0f)
{
// trigger whatever happens when the timer expires.
}
Davor's solution is fine, but I suggest you to use TimeSpan if you have to handle with time:
TimeSpan timer = new TimeSpan(0, 0, 1); //1 second
Then in the Update method:
if (timer > TimeSpan.Zero)
timer -= gameTime.ElapsedGameTime;
if (timer < TimeSpan.Zero)
{
timer = TimeSpan.Zero;
//load your play screen
}
timer = TimeSpan.Zero ensures that you load your play screen only once.
Related
I'm doing my first game with Lua in college and I'm having a hard time part.
My char shoots arrows non-stop and I want it to have a delay to shot each arrow.
I tried to create functions to simulate a delay but it did not work
local function atkRight()
system.setTapDelay(10)
display.remove(char)
char = display.newImageRect ( "Sprites/archerRight.png", 50, 60)
char.x = display.contentCenterX
char.y = display.contentCenterY+50
physics.addBody (char, "static", { isSensor=false })
char.myName = "char"
local arrowRight = display.newImageRect ( "Sprites/arrowRight.png", 50, 5)
arrowRight.x = display.contentCenterX+40
arrowRight.y = display.contentCenterY+40
physics.addBody (arrowRight, "dynamic", { bounce = 0 })
arrowRight:setLinearVelocity(500, 0)
arrowRight.gravityScale = 0
arrowRight.myName = "arrowRight"
end
atkiconRight:addEventListener( "tap", atkRight )
I wish this attack function could only be executed every 0.5 seconds
There are various ways to achieve this. The simplest way is probably to have your event callback check the time.
https://docs.coronalabs.com/api/library/system/getTimer.html
Store the time a shot was fired in a global variable.
When a shot is fired and there is a timestamp of a preceeding shot, check and only shoot if it is at least 0.5 seconds later.
Another way would be to remove the event listener and start a timer event that will re-add the event listener after 500ms. Or you have a global flag that prevents shooting and have a timer reset this flag every 500ms.
Which way to go is up to you.
I have a project in Sprite Kit and I am trying to make it so that a timer gradually decreases. For example if the timer float variable is set at 3.0, this would gradually decrease and be at 0, 3 seconds later. With the way that updates work in sprite kit its a horrible mess trying to get an integer to gradually decrease.
For example:
time+=1;
If I was to put this in an update void, it would increment extremely quickly and differently depending on frames and so forth. Is there a way I can Increment or Decrement a value at a steady rate no despite the fps in Sprite Kit?
You'd be better off getting the current time with each update and comparing it to some initial time to determine when 3 seconds pass.
Declare an ivar in your SKScene subclass:
#implementation MyScene {
NSDate* _timestamp;
}
When your timer starts:
_timestamp = [NSDate timeIntervalSinceReferenceDate];
Check your timer in your update pass:
- (void)update:(NSTimeInterval)currentTime {
if(_timestamp != nil && currentTime - _timestamp.timeIntervalSinceReferenceDate >= 3.0) {
// Perform your timer event
}
// Other updates
}
I have set up a match-3 game, and I want to track the number of seconds a user takes to touch valid points (game piece sprites) on the game board:
(1) when a game level is loaded, if the user does not touch a sprite within 5 seconds, track this;
(2) while playing the game, if the time elapsed between the user touching a sprite is greater than 5 seconds, track this.
(I will use these results to provide a hint to the user).
I would like to use a NSTimer/NSTimeInterval for this, but not sure how to implement it.
I wouldn't suggest adding an integer as others suggested.
All you need is delta time. I'm assuming you have that part worked out, but I'll post it just in case
scene properties
// time values
var delta:NSTimeInterval = NSTimeInterval(0)
var last_update_time:NSTimeInterval = NSTimeInterval(0)
your scene's update method (also create an update method for your sprites, and pass delta into it here)
func update(currentTime: NSTimeInterval) {
if self.last_update_time == 0.0 {
self.delta = 0
} else {
self.delta = currentTime - self.last_update_time
}
self.yourSprite.update(self.delta)
your sprite's time properties
var timeSinceTouched = NSTimeInterval(0)
let timeLimit = NSTimeInterval(5.0)
your sprites update / touched method
func touched(){
self.timeSinceTouched = 0.0
}
func update(delta: CFTimeInterval) {
if self.timeSinceTouched < self.timeLimit {
self.timeSinceTouched += delta
} else {
// five seconds has elapsed
}
So there is a UITextField and I have a limit of characters on the field. If user tries to enter anything bigger, UIView appears. I already have that part animated. Here is what I have:
public void animateHoursMesg() {
var HoursMesgExpandPos = new PointF (248.0f, 273.0f);
var HoursMesgInitPos = new PointF (400.0f, 273.0f);
UIView.Animate (
duration: 0.3f,
delay: 0,
options: UIViewAnimationOptions.CurveEaseInOut,
animation: () => {
if (Flag_HoursMesg) {
view_HoursLimitMesg.Center = HoursMesgInitPos;
Flag_HoursMesg = false;
} else {
view_HoursLimitMesg.Center = HoursMesgExpandPos;
Flag_HoursMesg = true;
}
},
completion: () => {
}
);
}
The problem is I want this view to animate away 5 seconds after it appears.
Can someone please help me with this?
Thank you for your time.
In your completion block on the animate method, use an NSTimer to schedule a block to execute 5 seconds from then.
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(animateAway)
userInfo:nil
repeats:NO];
Then just create another method for the animateAway that reverses your previous animation.
In the completion block of the animation, you can run a different animation to animate the view away with a delay of 5 seconds.
I've spent a few hours tonight trying to get two UILabels I have that are already being refreshed as a gesture is recognized (based on the translation.y and velocity.y). In the
if (recognizer.state == UIGestureRecognizerStateEnded)
I want to animate a deceleration of the refresh of the UILabels after the gesture is complete. The UILabels are refreshed just by calling a
[self refreshLabels];
I've spent a lot of time tonight trying to do this with an infinite scrollView and tracker of the .contentOffset.y which failed miserably. I also tried to do a for and while loop in the above if statement with an animation block which also didn't work.
Does anyone have any suggestions/done this before?
How about a recursive call of of refreshLabels using performSelector:withObject:afterDelay:?
When the gesture ends, set a private counter and call refreshLabels. Within refreshLabels, calculate a simple deceleration curve using the counter as the "time taken" variable and use the resulting value as the delay for the next recursive call.
Example code:
int counter; // Set this value to 1 when the gesture completes
- (void)refreshLabels:(id)sender{
// Update the labels using whatever code you have
// This code will execute the recursive deceleration
if (counter > -1){
float somereasonablenumber = 0.1F;
float delaytime = (1.0-0.1)/counter; // deceleration = (finaltime-initialtime)/timetaken
counter++;
if (delaytime > somereasonablenumber){
[self performSelector:#selector(refreshLabels:) withObject:nil afterDelay:delaytime];
} else {
counter = -1;
}
}
}
You might need to play with the values I used for finaltime and initialtime to get the curve you want. Right now, this code would execute on a delay curve like:
0.9s
0.45s
0.3s
0.225s
0.18s
0.15s
0.128571429s
0.1125s