How to decrease CCProgressTimer by 10 percent incrementally in cocos2d? - ios6

In my app, I want my "health bar" to decrease by 10 percent out of 100 percent each time my sprite collides with another sprite. I already have the collision stuff down. How would I do this? Here is the code in my init to create the progress timer:
CCSprite *healthFull = [CCSprite spriteWithFile:#"Health Bar.png"];
CCSprite *healthBorder = [CCSprite spriteWithFile:#"Health Bar Border.png"];
[self addChild:healthBorder z:5];
healthBorder.position = ccp(size.width / 2, 300);
health = [CCProgressTimer progressWithSprite:healthFull];
health.type = kCCProgressTimerTypeBar;
health.midpoint = ccp(1,0); // starts from right
health.barChangeRate = ccp(-1,0); // grow only in the "x"-horizontal direction
health.percentage = 100; // (0 - 100)
[self addChild:health];
health.position = ccp(size.width /2, 300);
And this is where I want to incrementally decrease the progress timer from 100 percent by 10 percent:
-(void)subtractLife{
}

-(void)subtractLife{
health.percentage-=10;
if(health.percentage<0)
health.percentage = 0;
}

Related

Fast Cumulative Sum?

The menu command "Volume > Projection > Project Along Z" is really fast as compared to scripting (even with intrinsic variables). Cumulative sum (projection) of a 3D image volume of 512x512x200 in z-direction takes <0.5 sec. as compared to >8 sec. by using script. Is there a direct access this script function other than using ChooseMenuItem()?
Script example showing the difference:
// create an image of 512x512x200, assign random numbers
image img := exprsize(512, 512, 200, random());
img.SetName( "test" );
img.ShowImage();
//
number start_tick, end_tick, calc_time;
// do volume projectin with menu command : Volume>Project>Project Along Z
start_tick = GetHighResTickCount();
ChooseMenuItem( "Volume", "Project", "Project Along Z"); // do z-projection
end_tick = GetHighResTickCount();
// calculate execution time
calc_time = CalcHighResSecondsBetween( start_tick, end_tick );
// display result image
Image img_projZ1 := GetFrontImage();
img_projZ1.SetName( "Z-proj.#1 (" + calc_time.format("%.2fs") + ")");
img_projZ1.ShowImage();
// do volume project in z-direction (using intrinsic variable iplane)
image img_projZ2 := exprsize(512, 512, 0.0);
start_tick = GetHighResTickCount();
img_projZ2[icol, irow, iplane] += img; // do z-projection
end_tick = GetHighResTickCount();
// calculate execution time
calc_time = CalcHighResSecondsBetween( start_tick, end_tick );
// display result image
img_projZ2.SetName( "Z-projection#1 (" + calc_time.format("%.2fs") + ")");
img_projZ2.ShowImage();
Using intrinsic variables is not the fastest way to go about this in scripting. (It used to be in GMS 1 a long time ago.)
In fact, if you do it as a for loop over slices you are a bit faster than with the command menu - most likely due to the overheads of calling that command and tagging the results.
// create an image of 512x512x200, assign random numbers
number sx = 512
number sy = 512
number sz = 200
image img := RealImage("",8,sx,sy,sz)
img = random();
img.SetName( "test" );
img.ShowImage();
number start_tick, end_tick, calc_time;
// do volume projectin with menu command : Volume>Project>Project Along Z
start_tick = GetHighResTickCount();
ChooseMenuItem( "Volume", "Project", "Project Along Z"); // do z-projection
end_tick = GetHighResTickCount();
// calculate execution time
calc_time = CalcHighResSecondsBetween( start_tick, end_tick );
// display result image
Image img_projZ1 := GetFrontImage();
img_projZ1.SetName( "Z-proj.#1 (" + calc_time.format("%.2fs") + ")");
img_projZ1.ShowImage();
// do volume project in z-direction (using intrinsic variable iplane)
image img_projZ2 := img_projZ1.ImageClone()
img_projZ2 = 0
start_tick = GetHighResTickCount();
for(number i=0; i<sz; i++)
img_projZ2 += img.slice2(0,0,i,0,sx,1,1,sy,1)
end_tick = GetHighResTickCount();
// calculate execution time
calc_time = CalcHighResSecondsBetween( start_tick, end_tick );
// display result image
img_projZ2.SetName( "Z-projection#1 (" + calc_time.format("%.2fs") + ")");
img_projZ2.ShowImage();
However, to answer your questions for a command: GMS 3.4 has a command
called:
RealImage Project( BasicImage img, Number axis )
RealImage Project( BasicImage img, Number axis, Boolean rescale )
void Project( BasicImage img, BasicImage dst, Number axis )
void Project( BasicImage img, BasicImage dst, Number axis, Boolean rescale )
But this command is not officially documented, so it might be renamed/removed at any time.

CoronaSDK - Remove timer from scene on click

I have a newCircle that when clicked changes the color of the circle and starts a timer it it. i want to be able to click the circle again and change it back to black and completely remove the timer from the scene.
delta = 0
local function tapListener( event )
if (delta == 0) then
c1:setFillColor(1,1,0)
local timeLimit = 20
timeLeft = display.newText(timeLimit, c1.x, c1.y, native.systemFontBold, 14)
timeLeft:setTextColor(255,0,0)
local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
print("Time Out") -- or do your code for time out
end
end
aTimer = timer.performWithDelay(1000,timerDown,timeLimit)
delta = delta + 1
else
c1:setFillColor(0,0,0)
delta = delta - 1
end
answer:
else
c1:setFillColor(0,0,0)
timer.cancel( aTimer )
timeLeft.alpha = 0
delta = delta - 1
end
the timer.cancal( aTimer ) stops the timer and the timeLeft.aplha = 0 hides the text that is displaying the timer

Is it possible to export a high res image from a Silverlight deepzoom site?

I'd like to download either the entire image, or at least a big part of it from here
http://www.xrez.com/yose_proj/yose_deepzoom/index.html
right now I'm zooming in, taking screen shots and tiling back together in Photoshop which is taking forever. Is there an easier way?
thanks.
i ended up wringing a glovepie script to move the mouse automatically, then just talk screen shots and use photoshops photomerge feature to put togethere. it works ok. here's the glovepie script
// script to press and drag mouse
if (Key.Left) {
debug = "start"
mouse.X -= .8
mouse.LeftButton = true
mouse.X += .8
wait 1 seconds
mouse.LeftButton = false
debug = "released"
}
if (Key.Right) {
debug = "start"
mouse.X += .8
mouse.LeftButton = true
mouse.X -= .8
wait 1 seconds
mouse.LeftButton = false
debug = "released"
}
if (Key.Up) {
debug = "start with the mouse near the bottom of the screen"
mouse.Y -= .8
mouse.LeftButton = true
mouse.Y += .8
wait 1 seconds
mouse.LeftButton = false
debug = "released"
}
if (Key.Down) {
debug = "start with the mouse near the top of the screen"
mouse.Y += .8
mouse.LeftButton = true
mouse.Y -= .8
wait 1 seconds
mouse.LeftButton = false
debug = "released"
}
download glovepie (geat tool) from here http://glovepie.org/glovepie_download.php
click on the "I already have 100% Green power, I'm ready to download GlovePIE" button. yep it sounds weird but it does work

JQuery TimeCircles - display minutes and seconds in one circle

I'm using the Jquery TimeCircles plug in (https://github.com/wimbarelds/TimeCircles) as a timer on my application, but I would like to have it so that there is only one circle with the minutes and seconds in the middle, like 15:40. I would like the seconds to keep ticking down, but the circle should animate according to the minutes only. Currently I have two circles showing the minutes and seconds.
I would like to start the timer at 50 minutes, and then countdown to 0 minutes and 0 seconds. Is there any way I can have the time display in the format MM:SS inside the one circle, and have the number of seconds ticking down, and the circle animating to the number of minutes ticking down only?
Thank you so much!
I actually had a fairly similar request the other day on github:
https://github.com/wimbarelds/TimeCircles/issues/68
You could change it to something like:
var $container = $('#DateCountdown .textDiv_Minutes');
$container.find('h4').text('Time left');
var $original = $container.find('span');
var $clone = $original.clone().appendTo($container);
$original.hide();
$('#DateCountdown').TimeCircles().addListener(function(unit, value, total) {
total = Math.abs(total);
var minutes = Math.floor(total / 60) % 60;
var seconds = total % 60;
if(seconds < 10) seconds = "0" + seconds;
$clone.text(minutes + ':' + seconds);
}, "all");
You'd need to use the TimeCircles options that make it only display the Minutes circle.

Creating buttons through iteration?

I've been using the Corona engine and I'm trying to create multiple buttons through a loop rather than explicitly creating each individual button. Problem is the loop only seems to be generating one button which suggests it's only iterating once.
Below is what I've got so far...
UPDATED
--> Create Level Selection:
local levelSelectionGroup = display.newGroup( );
--> Level Selected:
local function levelSelected()
print(id);
end
--> Button Creation:
local function createLevelSelection()
local levelsToBeMade = 30; -- Ignore these random numbers for now.
local positionX = 1; -- Ignore again.
local positionY = 1; -- Ignore again.
for buttonNumber=1, levelsToBeMade do
print(buttonNumber);
positionX = (positionX + 10); -- Ignore again.
positionY = (positionY + 10); -- Ignore again.
levelButton[buttonNumber] = widget.newButton{
id = buttonNumber,
label = buttonNumber,
default = "images/levelButton.png",
over = "images/levelButtonPressed.png",
width = 50,
height = 50,
onRelease = levelSelected
}
levelButton[buttonNumber].x = positionX;
levelButton[buttonNumber].y = positionY;
levelSelectionGroup:insert(levelButton[buttonNumber]);
end
end
The console states...
attempt to index global 'levelButton' (a nil value)
I guess you might have a problem with your variables or your scopes. So make sure that levelsToBeMade variable and positionX and positionY variables are correct. If you are absolutely sure, this should work: ( I don't see anything wrong in your code but, for loops are more trustable I guess. )
for i=1, levelsToBeMade do
print( "levelButton+1).." will be created." )
positionX = positionX + 10; -- Ignore numbers.
positionY = postionY + 10; -- Ignore numbers.
levelButton[#levelButton+1] = widget.newButton{
id = #levelButton,
label = #levelButton,
default = "images/levelButton.png",
over = "images/levelButtonPressed.png",
width = 50,
height = 50,
onRelease = levelSelected
}
levelButton[#levelButton].x = positionX;
levelButton[#levelButton].y = positionY;
end
If it doesn't work, just check console and see if the loop is executed desired times.
Last edit:
Oh, didn't notice that. You never created levelButton table before! Before starting to create level buttons you should create that like this: local levelButton = {}, at outside of for loop
You might want to reconsider your algorithm and change this while loop.
If the number of levels to be generated is going to stay constant you might want to do a for loop until the 31 st loop is reached.
for i=1,levelstToBeMade do
levelstToBeMade = (levelsToBeMade - 1); -- Ignore numbers.
positionX = positionX + 10; -- Ignore numbers.
positionY = postionY + 10; -- Ignore numbers.
levelButton[levelsToBeMade] = widget.newButton{
id = levelsToBeMade,
label = levelsToBeMade,
default = "images/levelButton.png",
over = "images/levelButtonPressed.png",
width = 50,
height = 50,
onRelease = levelSelected
}
levelButton[levelsToBeMade].x = positionX;
levelButton[levelsToBeMade].y = positionY;
end
Hope this helps.
Cheers
(Sorry, I don't know Corona so this might not apply.)
What is levelButton, and are you sure it exists? Maybe it should be levelButtons?
If that's fine as it is, then check to make sure newButton() actually returns a table as expected: print(levelButton[buttonNumber])

Resources