In coco2d game, update function is call every 1/60 of second.
this.scheduleUpdate();
update: function (dt) { }
The score Label is
var scoreLabel = cc.LabelTTF.create("0", "fantasy", 20, cc.size(0, 0), cc.TEXT_ALIGNMENT_LEFT);
scoreLabel.setPosition(cc.p(winSize.width - 80, winSize.height));
scoreLabel.schedule(function () {
var showingScore = parseInt(scoreLabel.getString());
if (showingScore < b2.getUserScore()) {
scoreLabel.setString((showingScore + 5)
.toString());
}
});
this.addChild(scoreLabel, 5);
How often is the scoreLabel being updated? How do you setup a timer to update every 2 seconds for the scoreLabel?
You can setup a schedule for this :
-(void) schedule: #SEL(selector) interval: ccTime ;
selector : your function that you want to update .
interval : time interval between each update .
you could once initialize Label then in a function (for example "Update Label") change it's
attributes .
Related
I have a question regarding timers and publishers. I'm working with a timer that fires every second and does a small calculation and an update of two published values for counting down in the UI.
I've put everything that is not UI related on background threads and when the timer only runs the background code the interval is fine at 1.000 with some tolerance. But when the timer is also updating the two published values on the main thread the interval is between 1.015 and 1.019.
That doesnt sound much but after 20 minutes the total offset is at about 20 seconds.
Is there a best practice or a solution for that?
View:
#EnvironmentObject var timer: TimerData
let counter = Timer.publish(every: 1.0, tolerance: 0.2, on: .main, in: .common).autoconnect()
...
.onReceive(counter) { _ in
DispatchQueue.global(qos: .userInteractive).async {
timer.countingTime()
}
}
Model:
class TimerData: ObservableObject {
#AppStorage("time") var time: Int = 0
#AppStorage("lastAction") var lastAction: Double = 0
func countingTime() {
let now = Double(NSDate().timeIntervalSince1970)
let diff = Int(round(now - self.lastAction))
print(now - self.lastAction)
DispatchQueue.main.async {
self.lastAction = now
self.time -= diff
}
}
}
I've tried no tolerance value when creating the timer as well as a higher tolerance value. The only way I found to get the interval to the estimated 1.000 is to make "time" and "lastAction" not published (#AppStorage or #Published didnt made a difference). But I need both values published (I guess).
I'm reading docs but there seems to be no parameters to do what i need: I have a plain bar chart and I need to make some bars blink depending on threshold configuration. Is there any plugin or secret parameter to obtain this?
As far as I understand, you want to have a blinking bar.
So what you can do is create the bar and then dynamically update its background-color from 'transparent' to the original color periodically using a timer.
This will create a blinking effect.
Ok, so if you want the first bar to blink:
let count = 0;
setInterval(() => {
if(count % 2 === 0) {
myChart.data.datasets[0].backgroundColor[0] = 'transparent';
myChart.update();
}
else {
myChart.data.datasets[0].backgroundColor[0] = '#FFC857';
myChart.update();
}
count++;
}, 2000);
I don't know if there is a better solution for it, but this is the first thing that came to my mind.
I want to call a method at Irregular time interval means it should be random time plus i want it in some define range too.
Like : it should call at any second between 3 to 8 .
I tried this one :
[NSTimer scheduledTimerWithInterval: 1.0 target:self selector:#selector(myMethod:) userInfo:nil repeats: YES];
void mymethod()
{
if(arc4random() % 10 == 1)
{
// calling my method here;
}
}
This way , i am not getting randomization which i want.
Any one can please help me on this !!!
Here you can make a scheduler which will get called at random interval.
-(void)randomTimeScheduler{
int time = arc4random()%5;
int nextTimeOfCall = 3+time;
NSLog("it will be called after:%d",nextTimeOfCall);
[self performSelector:#selector(randomTimeScheduler) withObject:self afterDelay:nextTimeOfCall];
}
You have to call it from your class and then it will work as a scheduler. And it has finite interval time 3-8.
I'm trying to write bukkit plugin and one of the features I would like to add is when a player drops to Y level 8 or lower it runs a command on that player. My current case is I'd like to teleport them to other coordinates if they drop to Y level 8 or lower.
Is there a lite way of doing this without have a command run on every "move" event getting each players location then getting their y position then checking if it's less than 9? cause that seems like it would be a lot of work on the server if there are many people moving around.
There is a simpler way to do this without using runnables. Simply use a PlayerMoveEvent.
#EventHandler
public void onMove(PlayerMoveEvent event) {
if (event.getPlayer().getLocation().getY() <= 8) {
event.getPlayer().teleport(event.getPlayer().getLocation().add(0, 100, 0));
}
}
You could always use the SyncRepeatingTask. Then loop through all online players and check if their Y coordinate value is less than 9:
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
public void run() {
for (Player player: Bukkit.getOnlinePlayers()) { // Loops through all online players
if (player.getLocation().getY() < 9) {
// The player's Y is below 9. Do whatever you want here. For example:
player.teleport(player.getLocation().add(0, 100, 0)); // Teleports the player 100 blocks above where they are
}
}
}
}, 50L, 50L); // Run every 2.5 seconds (50 ticks)
Just make sure to put the above code in your onEnable(), or a method that's called when your plugin is enabled.
If you want to change the repeat time dynamically based on the online players, you could use:
public void runTeleportTask() {
long time = Math.round(Bukkit.getServer().getOnlinePlayers().length / 10) + 10;
/*
Tweak the delay however you like, above we get the online players, and divide it by 10 then
add ten. We're adding 10 because you don't want to end up with a task that runs every
tick in this case... The fastest it would be needed would be every 10 ticks.
So if there were 100 players online, it would get 100/10 = 10, 10+10 = 20 = 1 second,
and if there were 250 players online, it would get 250/10 = 25, 25 + 10 = 35 = 1.75 seconds
*/
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.getLocation().getY() < 9) {
player.teleport(player.getLocation().add(0, 100, 0));
}
}
runTeleportTask();
}
}, time);
}
Then all you have to do is invoke runTeleportTask() in your onEnable() method.
I have two schedule in class #A.
first one:
- (id) init ()
{
self = [super init];
if ( self ) {
....
[self schedule:#selector(first:) interval:1.0f];
}
}
second:
- (void) setSomething()
{
...
[self schedule:#selector(second:) interval:1.0/30];
}
the second schedule processed correctly.But the first one can not be called once.
And when I set interval of first schedule from 1.0f to 0.The first one could be called.
How can I do for the first schedule?
Dude,check your method name that was called by first scheduler.It should be like
-(void)first:(id)sender
{
....
}