I tried the lightning. But for whatever reason it does not start its fadeout action. I am using actions an many places and all are working. No idea whats up with this one.
Help with Electricity
(73 posts) (42 voices)-
Posted 1 year ago #
-
Are you seeding it? It happened to me as well when I wasn't.
Posted 1 year ago # -
@zombie, @jandrad - That's a strange issue.... setting the seed shouldn't really matter as it just needs to be ANY value. Is it just always staying visible for you? or is it just disappearing with no fade?
Ha, I just also noticed that there's a bunch of header files included and a variable that's unnecessary... I'll clean that up right away!
Posted 1 year ago # -
Hi, thanks for the lightning code & I'll be sure to credit you in my game.
I tinkered around with it and came up with a cool electric shock effect.
In Lightning.m, I changed drawLightning to:CGPoint drawLightning(CGPoint pt1, CGPoint pt2, int displace, int minDisplace) { CGPoint mid = ccpMult(ccpAdd(pt1, pt2), 0.5f); if ( displace < minDisplace ) { ccDrawLine(pt1, pt2); } else { int r = random(); mid.x += (((r % 101) * 0.01f) - 0.5f) * displace; r = random(); mid.y += (((r % 101) * 0.01f) - 0.5f) * displace; drawLightning(pt1, mid, displace >> 1, minDisplace); drawLightning(pt2, mid, displace >> 1, minDisplace); } return mid; }Then to use, I initialized with:
` //seed random routine one time
// you can change this init seeding to something else if you like
long pid = getpid();
srandom(time(0) + pid + (pid << 15) );lightning = [Lightning lightningWithStrikePoint:ccp(50, 310)];
lightning.position = ccp(440, 20);
ccColor3B blueL;
blueL.r = 120.0f;
blueL.g = 170.0f;
blueL.b = 255.0f;
lightning.color = blueL;
lightning.split = YES;
lightning.strikePoint2 = ccp(40, 300);
lightning.visible = FALSE;
[self addChild:lightning];`… at the point where the electric shock is turned on :
lightning.visible = TRUE; [lightning strikeRandom];you could have this called repeatedly by a timer if you need continuous electricity.
make sure you change the drawLightning calls to leave off the last parameter in Lightning.m, change the prototype to
CGPoint drawLightning(CGPoint pt1, CGPoint pt2, int displace, int minDisplace) and in the Lightning.h file.and don't forget to cleanup when deallocating on exit:
if ( lightning ) { [self removeChild:lightning cleanup:YES]; }Posted 1 year ago # -
Hi I have a problem wit your lightning , mobilebros. I am using cocos2d 0.99.5 and use your code as follows
// // HelloWorldLayer.m // TestLightning // // Created by Samuel Kitono on 24/11/10. // Copyright __MyCompanyName__ 2010. All rights reserved. // // Import the interfaces #import "HelloWorldScene.h" #import "Lightning.h" // HelloWorld implementation @implementation HelloWorld +(id) scene { // 'scene' is an autorelease object. CCScene *scene = [CCScene node]; // 'layer' is an autorelease object. HelloWorld *layer = [HelloWorld node]; // add layer as a child to scene [scene addChild: layer]; // return the scene return scene; } // on "init" you need to initialize your instance -(id) init { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if( (self=[super init] )) { /* // create and initialize a Label CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64]; // ask director the the window size CGSize size = [[CCDirector sharedDirector] winSize]; // position the label on the center of the screen label.position = ccp( size.width /2 , size.height/2 ); // add the label as a child to this Layer [self addChild: label]; */ [self schedule:@selector(nextFrame:)]; } return self; } - (void) nextFrame:(ccTime)dt { // [lightning strike]; Lightning *lightning = [Lightning lightningWithStrikePoint:ccp(100,50)]; lightning.position = ccp(200,50); [self addChild:lightning]; [lightning strike]; [self removeChild:lightning cleanup:YES]; } // on "dealloc" you need to release all your retained objects - (void) dealloc { // in case you have something to dealloc, do it in this method // in this particular example nothing needs to be released. // cocos2d will automatically release all the children (Label) // don't forget to call "super dealloc" [super dealloc]; } @endthere is no lightning being drawn at all in the scene. Can you help me?
Posted 1 year ago # -
Hmmm, well your code looks simple enough... I'll give it a try with 0.99.5 rc1 and let you know how it goes.
Posted 1 year ago # -
Oh wait a second here..... You are "scheduling" nextFrame to run every 1/60th of a second which is wayyyyyy to fast. Also, you are adding the lightning and then IMMEDIATELY removing it. That's why you don't see anything.
What kind of effect are you going for here? You might actually be interested in the Tesla class that comes with it; it's a simple derivation of lightning that whose strikepoint is a given node's position and it has a constantly changing seed value so it's like a crazy electricity surge.
Posted 1 year ago # -
I got it to work using this:
lightning = [Lightning lightningWithStrikePoint:ccp(50, 610)]; lightning.position = ccp(440, 20); ccColor3B blueL; blueL.r = 120.0f; blueL.g = 170.0f; blueL.b = 255.0f; lightning.color = blueL; lightning.split = YES; lightning.strikePoint2 = ccp(40, 600); [self addChild:lightning]; [lightning strike];When I use it like this the lightning shows up and disappears. So that works.
When I add
[self removeChild:lightning cleanup:YES];after
[lightning strike];nothing shows up. Is there somewhere else that I should be putting this?
How would you get the lightning to show up at random different coordinates every-time it splinters off.
I tried something like this:
id fade = [CCFadeIn actionWithDuration:1.0f]; id fade_back = [fade reverse]; id seq = [CCSequence actions: fade, fade_back, nil]; id repeat = [CCRepeatForever actionWithAction:seq]; [lightning runAction:repeat];It works but it's not the effect that I was trying to get. This just repeats the same lightning over and over. I want that every-time that the lightning fades in again it splinters off in different directions. I'm using it in a background and it wold make it visually interesting if every-time that it flashed back on it would be different.
Also, is it possible to vary the thickness of the lightning, say thicker to thinner?
Sorry for all the questions. Thanks for all your help.
Demetrius
Posted 1 year ago # -
Lightning is like any other CCNode, it must be added to be seen, if you remove it, you are effectively killing it. It "disappears" by just doing a fadeout on itself. If you want random strike points and/or positions and such you must change them manually between "strikes" (by the way strike doesn't do much beside "show" itself and then fadeout).
Oh, the seed is important in how the lightning "randomly" zags, so calling strikeRandom or strikeWithSeed will give you more random zagging each time (make sure the seed is different each time if you use that latter method).
If you want the lightning thicker you'll have to change the src code.... the glLineWidth(1.0f) in the draw method is what you want to change.
I think I got all your questions, but let me know if you need more clarification.
Posted 1 year ago # -
I'm not having a lot of luck with this, or at least it's not working how I thought it was supposed to by reading all the info here. I'm running a game in portrait mode. I have a bunch of sprites, and I basically want to radiate lightning from 1 sprite to a bunch of other sprites. I always seem to have the lightning come in from the top right corner and hit the sprite I want to radiate from. Not quite the effect I was looking for. But if I move the position to the position of each of the sprites I'm trying to strike, then I get lightning striking each of the sprites. But the origin point is still the top right corner. It never seems to matter what strikePoint is, only Lightning position seems to matter.
Let's say I have sprite1 (the sprite I want to strike from) at position 160, 240 and I have sprite2 at position 20, 20. Then let's say my code looks like so:
Lightning *l = [Lightning lightningWithStrikePoint:ccp(20, 20)]; l.position = ccp(160, 240); [self addChild:l]; // self is the GameScene which is a CCLayer [l strikeRandom];What happens in this case is I get a strike from the top right corner (which I expect to be 360, 480) to 160, 240. I thought it should go from 160, 240 to 20, 20. It never seems to matter what the value of strikePoint is. I've even changed it to 160, 240 (the same as position), and it has absolutely no effect. Did anyone else have this problem?
EDIT: Okay, strikePoint seems to have some affect, changing it to radical values seems to do some strange things, but never going from position to strikePoint.
Posted 1 year ago # -
Okay, figured out my issue. I didn't realize ccDrawLine's point system was based on the position of the CCNode being ccp(0,0). So, to fix my issue, I just needed to ccpSub my position from my strikePoint. Basically, the draw is relative to the position of the starting point of the Lightning. Still a bit of a noob on some cocos2d functions.
Posted 1 year ago # -
So i have tried adding a glow to the bolt with no succes, i thought the easiest way was to offset another bolt one pixel and then turn down it's opacity (repeated like 4 times on both sides). Not good... So is it possible to add a shader to the bolt inorder to add the desired glow effect? I suck at openGL...
Thanks!
Posted 1 year ago # -
Bump :-/)
Posted 1 year ago # -
Mobilebros, I'm still having the same problem that sosergio had that has to do with the page turn transition getting screwed up colorwise by the OpenGL calls in your lightning.m file. I'm using the latest code that you have on your site but it still happens. I'm using cocos2d 1.0. Can you take a look?
Posted 1 year ago # -
You may need to reset the drawing defaults. Add this at the end of your draw function in Lightning.m
CC_ENABLE_DEFAULT_GL_STATES();
Posted 1 year ago # -
sunQ.I'm using it.I will change the source code, make it more powerful.Very thanks!!
i use cocos2d 0.995.
I did not find any problems.
Only Lines are not too pretty,but it is enough.
Thanks!!Love you
Posted 1 year ago # -
@MobileBros, thanks for posting this.
I've made a small improvement, the lines are now batch drawn.
.h
// // Lightning.h // Trundle // // Created by Robert Blackwood on 12/1/09. // Copyright 2009 Mobile Bros. All rights reserved. // #import "cocos2d.h" #define kMaxPoints 200 /*! This will draw lighting and return the midpoint */ CGPoint drawLightning(CGPoint pt1, CGPoint pt2, int displace, int minDisplace, unsigned long randSeed, CGPoint* points, uint* numPoints); @interface Lightning : CCNode<CCRGBAProtocol> { CGPoint _strikePoint; CGPoint _strikePoint2; ccColor3B _color; GLubyte _opacity; BOOL _split; int _displacement; int _minDisplacement; unsigned long _seed; CGPoint *lightningPoints_; uint numPoints_; } @property (readwrite, assign) CGPoint strikePoint; @property (readwrite, assign) CGPoint strikePoint2; @property (readwrite, assign) ccColor3B color; @property (readwrite, assign) GLubyte opacity; @property (readwrite, assign) BOOL split; @property (readwrite, assign) int displacement; @property (readwrite, assign) int minDisplacement; @property (readwrite, assign) unsigned long seed; +(id) lightningWithStrikePoint:(CGPoint)strikePoint; +(id) lightningWithStrikePoint:(CGPoint)strikePoint strikePoint2:(CGPoint)strikePoint2; -(id) initWithStrikePoint:(CGPoint)strikePoint; -(id) initWithStrikePoint:(CGPoint)strikePoint strikePoint2:(CGPoint)strikePoint2; -(void) strikeRandom; -(void) strikeWithSeed:(unsigned long)seed; -(void) strike; @end.m
// // Lightning.m // Trundle // // Created by Robert Blackwood on 12/1/09. // Copyright 2009 Mobile Bros. All rights reserved. // #import "Lightning.h" #import "CCActionInterval.h" #import "CCActionInstant.h" @implementation Lightning @synthesize strikePoint = _strikePoint; @synthesize strikePoint2 = _strikePoint2; @synthesize color = _color; @synthesize opacity = _opacity; @synthesize displacement = _displacement; @synthesize minDisplacement = _minDisplacement; @synthesize seed = _seed; @synthesize split = _split; +(id) lightningWithStrikePoint:(CGPoint)strikePoint { return [[[Lightning alloc] initWithStrikePoint:strikePoint] autorelease]; } +(id) lightningWithStrikePoint:(CGPoint)strikePoint strikePoint2:(CGPoint)strikePoint2 { return [[[Lightning alloc] initWithStrikePoint:strikePoint strikePoint2:strikePoint2] autorelease]; } -(id) initWithStrikePoint:(CGPoint)strikePoint { return [self initWithStrikePoint:strikePoint strikePoint2:ccp(0,0)]; } -(id) initWithStrikePoint:(CGPoint)strikePoint strikePoint2:(CGPoint)strikePoint2 { [super init]; _strikePoint = strikePoint; _strikePoint2 = strikePoint2; _color = ccWHITE; _opacity = 255; _seed = rand(); _split = NO; _displacement = 160; _minDisplacement = 8; numPoints_ = 0; lightningPoints_ = (CGPoint*) malloc(sizeof(CGPoint)*kMaxPoints); [self strike]; return self; } - (void) dealloc { [super dealloc]; } -(void) draw { numPoints_=0; glColor4ub(_color.r, _color.g, _color.b, _opacity); glLineWidth(3.0f); glEnable(GL_LINE_SMOOTH); if (_opacity != 255) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); CGPoint mid = drawLightning(ccp(0,0), _strikePoint, _displacement, _minDisplacement, _seed,lightningPoints_, &numPoints_); if (_split) drawLightning(mid, _strikePoint2, _displacement/2, _minDisplacement, _seed, lightningPoints_, &numPoints_); ccDrawLines(lightningPoints_,numPoints_); if (_opacity != 255) glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); } -(void) strikeRandom { _seed = rand(); [self strike]; } -(void) strikeWithSeed:(unsigned long)seed { _seed = seed; [self strike]; } - (void) strike { self.visible = NO; self.opacity = 255; [self runAction:[CCSequenceNew actions: // [DelayTime actionWithDuration:1.0], [CCShow action], [CCFadeOut actionWithDuration:0.5], // [CallFunc actionWithTarget:self selector:@selector(strikeRandom)], nil]]; } @end void ccDrawLines( CGPoint* points, uint numberOfPoints ) { //layout of points [0] = origin, [1] = destination and so on ccVertex2F vertices[numberOfPoints]; if (CC_CONTENT_SCALE_FACTOR() != 1 ) { for (int i=0;i<numberOfPoints;i++) { vertices[i].x=points[i].x * CC_CONTENT_SCALE_FACTOR(); vertices[i].y=points[i].y * CC_CONTENT_SCALE_FACTOR(); } glVertexPointer(2, GL_FLOAT, 0, vertices); } else glVertexPointer(2, GL_FLOAT, 0, points); // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY // Needed states: GL_VERTEX_ARRAY, // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY glDisable(GL_TEXTURE_2D); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDrawArrays(GL_LINES, 0, numberOfPoints); // restore default state glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D); } int getNextRandom(unsigned long *seed) { //taken off a linux site (linux.die.net) (*seed) = (*seed) * 1103515245 + 12345; return ((unsigned)((*seed)/65536)%32768); } CGPoint drawLightning(CGPoint pt1, CGPoint pt2, int displace, int minDisplace, unsigned long randSeed, CGPoint *points, uint *numPoints) { CCLOG(@"arguments pt1 %f %f pt2 %f %f dis %i minDis %i",pt1.x,pt1.y,pt2.x,pt2.y,displace,minDisplace); CGPoint mid = ccpMult(ccpAdd(pt1,pt2), 0.5f); if (displace < minDisplace) { //ccDrawLine(pt1, pt2); if (*numPoints == kMaxPoints - 1) CCLOG(@"increase kMaxPoints"); points[*numPoints]=pt1; points[*numPoints+1]=pt2; *numPoints+=2; } else { int r = getNextRandom(&randSeed); mid.x += (((r % 101)/100.0)-.5)*displace; r = getNextRandom(&randSeed); mid.y += (((r % 101)/100.0)-.5)*displace; drawLightning(pt1,mid,displace/2,minDisplace,randSeed,points,numPoints); drawLightning(pt2,mid,displace/2,minDisplace,randSeed,points,numPoints); } return mid; }Posted 1 year ago # -
@MobileBros && @araker Thank you for the posting the code!
Posted 1 year ago # -
Can't wait to try this out later, thanks a lot for sharing the code!
Anyone want to share some screen shots in the mean time? :p
Posted 1 year ago # -
Thank you for the code. I managed to use the lightning effect but I have difficulty in creating the tesla. May I know what how to use the tesla?
Posted 1 year ago # -
Okay, figured out my issue. I didn't realize ccDrawLine's point system was based on the position of the CCNode being ccp(0,0). So, to fix my issue, I just needed to ccpSub my position from my strikePoint. Basically, the draw is relative to the position of the starting point of the Lightning. Still a bit of a noob on some cocos2d functions.
As per what smeep2k4 mentioned before, the strike point you specify is NOT an absolute position, but a point that is RELATIVE to the lightning's position. I got confused for 10 minutes thinking I had an orientation problem :)
BTW, this runs pretty slow (3FPS) on the 2nd gen iPod Touch. I'm guessing it's expected considering there's a pretty heavy recursive function being called on every draw. Am I correct?
Posted 1 year ago # -
Int X=sprite.x; Int Y=sprite.y Lightning *l = [Lightning lightningWithStrikePoint:ccp(touch.x - X, touch.x - Y)]; l.position = ccp(X, Y); [self addChild:l]; // self is the GameScene which is a CCLayer [l strikeRandom];Would that work if I wanted make it zap from sprite to touch position.
Posted 1 year ago # -
@SunnyPianist - 3 FPS sounds wayy too slow, but I guess if the recursion was ridiculously deep...
@me4502 - how about
CGPoint toPt = [sprite convertTouchToNodeSpace:touch]; Lightning *l = [Lightning lightningWithStrikePoint:toPt]; l.position = sprite.position; [self addChild:l]; [l strikeRandom];where touch is a UITouch* and sprite is some CCNode* type
Posted 1 year ago # -
Yea that's what I figured too. I didn't look into it deep enough to find out why yet. I'm using Araker's code above, haven't checked what he changed from yours. The code I wrote is pretty simple, something to the effect below (quickly written, may not compile):
MyLayer.m
-(id)init { if(self = [super init]) { lightning = [Lightning lightningWithStrikePoint:ccp(0, -100)]; lightning.position = ccp(160,460); //Portrait mode lightning.color = ccBLUE; [self addChild:lightning]; [self schedule:@selector(update:) interval:5.0f]; } } -(void) update:(ccTime)dt { [lightning strikeRandom]; }If lightning wasn't added, then it's 60FPS. Once lightning is added, drops down to 3FPS. Pretty sure it's that draw function slowing everything down, but i'll look into it again later tonight.
Posted 1 year ago # -
Thanks. I'll try that ASAP
Posted 1 year ago # -
Thanks MobileBros! I just got my first iOS Cocos2D game approved and it's in the App Store now. It's called Warp Jam and I'm using the lightning. I credited you in the description. Thanks so much!
Posted 12 months ago # -
hello
mobilebros.. i tried your lightning code and its amazing..well though i would like to know if i want to put glow in it..as to how would it be done
Thanks
Posted 11 months ago # -
I used it here too. Great work and thanks!!
Star Connect
Posted 11 months ago #
Reply »
You must log in to post.