Is there a way to combine ease-in and linear in the CSS3 -animation-timing-function? - css-transitions

I am asking because I am working on making a vinyl record ease into motion, and then it needs to rotate at constant speed. I can make it rotate at contant easily, and ease-in-out easily by adding these classes:
#label_vinyl.rotate {
-webkit-animation-name: rotate;
-webkit-animation-duration: 12s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}
#label_vinyl.ease {
-webkit-animation-name: rotate;
-webkit-animation-duration: 12s;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: ease-in;
}
my problem is making the switch from one class to the other. When I try and replace the class ease class with the rotate class. Or if i have both classes added first (making the ease-in override since it is last), and then removing the ease class, it still has the 'easing animation', even when the ease class is no longer attached to my div/img.
It is as if the ease-in sticks forever even when i change classes. My question is: is there a way to combine the two or chain them? something on the lines of:
-webkit-animation-iteration-count: 1, infinity;
-webkit-animation-timing-function: ease-in, linear;
the former does not work btw.
EDIT: if there isnt, there really should be! :)

Can you just use jQuery to control the classes?
$('#label_vinyl').toggleClass('ease','rotate');
or were you wanting to do this all CSS?

Related

Clarification on the Dependency Inversion Principle

Excuse me for cross-posting on Software Engineering, didn't know that it was frowned upon.
The answer I was got there was exactly what I was looking for, for those curious: https://softwareengineering.stackexchange.com/a/347143/269571
Original question
I'm reading the book "Agile Software Development, Principles, Patterns, and Practices" by Robert C. Martin.
When he talks about the Dependency Inversion Principle he gives the following example of a DIP violation:
This seems very clear to me, as the higher-level object Button is depending on a lower-level object Lamp.
The solution he comes with is:
He creates an interface so that way Button is no longer depending on the object Lamp.
The theory seems really clear to me, however I can't wrap my head around using the principle in real-life projects.
Who is going to determine what classes (that implement SwitchableDevice) need to be called?
Who tells Button what devices he need to turn on/off?
How do you tell an object that uses something abstract which concrete things it needs to use? (Please correct me if this question is completely wrong)
If anything is unclear about my question, please let me know, I'll be glad to clarify things for you.
The whole point of dependency injection (at least as I understood it) is that the Button does not need to know what concrete SwitchableDevice it is switching.
The abstract interface could look like this:
struct SwitchableDevice {
virtual void switchOn() = 0;
virtual void switchOff() = 0;
};
And the button could be implemented like this:
struct Button {
SwitchableDevice& dev;
bool state = false;
Button(SwitchableDevice& d) : dev(d) {}
void buttonPress(){
if (state) { dev.switchOff(); }
else { dev.switchOn(); }
state = !state;
}
};
For the button, thats it! Noone needs to tell the button what is the concrete implementation of the SwitchableDevice, in other words: The implementation of the Button and the SwitchableDevice are decoupled.
A possible implementation of a Lamp could look like this:
struct Lamp : SwitchableDevice {
void switchOn(){std::cout << "shine bright" << std::endl;}
void switchOff(){std::cout << "i am not afraid of the dark" << std::endl;}
};
And that could be used like this:
int main(){
Lamp lamp;
Button button(lamp);
button.buttonPress();
button.buttonPress();
}
Hope that helps...
The benifit is that now we can change the implementation of the Button and the Lamp individually, without having to change anything on the other part. For example a ButtonForManyDevices could look like this:
struct ButtonForManyDevices {
std::vector<SwitchableDevice*> devs;
bool state = false;
Button(std::vector<SwitchableDevice*> d) : devs(d) {}
void buttonPress(){
if (state) for (auto d: devs) { d.switchOff(); }
else for (auto d: devs) { d.switchOn(); }
state = !state;
}
};
And similarly you could change the behaviour of the Lamp completely (of course within the limits of SwitchableDevice without having to change anything on the button. The same ButtonForManyDevices could even be used to switch a Lamp, a VaccumCleaner and a MicroWaveOven.
He's saying that what a button controls should be more generalized than just a lamp. If you have button classes for each type of thing that a button can control, then you could wind up with a lot of button classes.
In the first example, one is describing a button on a lamp. It essentially is taking the lamp as the starting point and dividing it into components.
In the second example, he is dividing the parts and looking at a button more generally.
Who is going to determine what classes (that implement SwitchableDevice) need to be called?
There is going to have to be a link between the button and the interface.
Who tells Button what devices he need to turn on/off?
The Button class would need to implement a mechanism to tell what device it is connected to.
How do you tell an object that uses something abstract which concrete things it needs to use? (Please correct me if this question is completely wrong).
Because an object that derives from an abstract interface must fully implement the interface. A Lamp object must define a TurnOn and TurnOff method somewhere..

Add css transition on svg element class changing

I'm exploring the markAvailable property of this example.
I would like to add a transition while the ports color change.
So I changed the css as follow:
/* port styling */
.available-magnet{
fill: yellow;
transition: fill 0.5s;
}
.port circle{
transition: fill 0.5s;
}
But I still do not get any transition.
UPDATE
I created an example. It is clear that the problem is inside defaultLink property. In particular, removing the z attribute I get the right transition, but in this way I lost a fundamental property for my need, that is indeed to have all links with the lower z index.
The Example reported in the Update is correct.
In order to maintain each link on the back of other elements (that corresponds to set a z value equals to -1), Simply use the method link.toBack() each time a new link is created. In this way the transitions work.
Use -webkit-transition if you are in Chrome if you are in Mozilla use -moz-transition

Synchronous CCLayers - running levels in complex defined order

I'm building a game that has dozens of dynamically generated mini-levels. To simplify, let's say I've got three types of levels, A, B, and C, and each one can take an integer difficulty. LevelA, LevelB, and LevelC are subclasses of CCLayer, and I've been loading them into a GameScene that's a subclass of CCScene, one at a time. I'm not married to this method; it's just what's been working.
What I can't figure out is how to control the flow of levels in an overarching way.
The simple way to load a new level seems to be replicate the [[CCDirector sharedDirector] replaceScene:[newScene node]] model - so something like: [[GameScene sharedScene] replaceGameplayLayer:[LevelA newLevelWithDifficulty:5]]].
This works fine, but only if the call to the next level comes within the code for the current level. So if I'm in an A-level, I can load up a corresponding B-level every time I hit 10 points - something like:
-(void)checkScore {
if (score >= 10) {
[[GameScene sharedScene] replaceGameplayLayer:[LevelB newLevelWithDifficulty:currentLevelDifficulty]];
}
}
And then do the same in the LevelB class to load up C-levels, or some other configuration.
But what if I want to plan out a level order? A1, B1, C1, C2, C3, C4, C5, A2, etc? I'd like to be able to treat each level as a synchronous line in a function. Like:
-(void)playGame {
[LevelA newLevelWithDifficulty:1];
[LevelB newLevelWithDifficulty:1];
for (int i = 0; i < 5; i++) {
[LevelC newLevelWithDifficulty:i];
}
// ...etc...
}
I'm not sure how to tackle the problem though. Is it even possible? It's easy enough to make that checkScore method just kill the current Level at 10 points, but how would I keep LevelB from happening until it did?
I hope this question makes sense. Any help you could offer would be appreciated.
you could create in your scene an array of levels in the appropriate sequence ... but creating them all ahead of time would be expensive (textures, etc ...). So devise a key system that uniquely identifies the level and difficulty. Next, your scene could register for a notification (read up on NSNotificationCenter and NSNotification), and each level could signal completion by posting a notification. You could have multiple notifications, for example one for levelComplete, gameOver and gameVictory.
When the scene gets the levelComplete notification, you could fade out the layer, look up the next level info, instanciate an instance of the class, add it, and fade it in. ....
just one way of doing something like that. Others available.
I have level descriptor plists that contain the class name, the music to play, victory and gamover conditions, etc .... and use a scheme like this to sequence levels without swapping scenes. Works for me. ymmv :)

C++ parallel loops

I'm making a console game called alien spaceships as a homework. It should look something like this http://img74.imageshack.us/img74/8362/alieninvadersfdcl192720mu1.jpg .
So far so good I ain't allowed to use classes nor objects => only functions and arrays.
I have one while loop that checks the buttons I press on the keyboard and according to the button applies some functions.
The problem comes when I try to shoot a missle because it's done with a "for" loop and when I shoot I can't move. Can someone give me an idea how the model is supposed to look like and how can I make something like this work. I don't think it's needed to post my code, but if you want I'll post it.
I assume that you're not willing to play with multiple threads. It is not mandatory for a simple game like this and would add a bit of complexity.
So generic loop for monothreaded game is:
state new_state = createInitialState();
do
{
input = readInput(); // non blocking !
new_state = modifyState(input, new_state);
updateScreen(new_state);
}
while (!exitCondition(input));
None of these functions should loop for long.
In your case, the missile position should be updated in modifyState taking into account the time since the last modifyState.
I assume you use a matrix to store all the data, and periodically you print the content of the matrix (that's how you create a console game).
So, your code should look something like this:
render()
{
update_position(x,y);
if(missile_fired)
update_missile_position();
}
main()
{
for(;;)
{
read_input(&x,&y);
render();
draw_image();
}
}

Cocos2d with ARC: what is the best implementation of singleton pattern for GameScene when having multiple levels?

EDIT: I am using ARC
In my code (based on the ShootEmUp example in this book, which I highly reccomend, source code in chapter 8 available here) I often use the trick of accessing the GameScene via:
+(GameScene*) sharedGameScene;
which returns a reference to the static instance of GameScene and is used by all children of GameScene (e.g. ShipEntity, InputLayer.. etc..) to dialog with GameScene (EDIT: aka singleton instance of GameScene).
To create multiple levels I thought of implementing a method calledsceneWithId:(int) method where I load different level data each time.
EDIT: Code snippet of init method:
-(id) initWithId:(int)sceneId
{
CCLOG(#"scene With id");
if ((self = [super init]))
{
//All common initialization happens here..
switch (sceneId) {
case 1:
[self loadFirstLevelData];
break;
case 2:
[self loadSecondLevelData];
break;
default:
[self loadSecondLevelData];
break;
}
//Other common stuff..
[self setUpCounters];
[self setUpWeaponsMenu];
[self scheduleUpdate];
[self schedule:#selector(timerUpdate:) interval:1];
InputLayerButtons* inputLayer = [InputLayerButtons node];
[self addChild:inputLayer z:1 tag:GameSceneLayerTagInput];
}
EDIT: Is that init method ok? I have found this post which uses dispatch_once. Should I do the same?
Or should I pheraps create a GameScene class and then sublcass it?
E.g. FirstGameScene : GameScene
EDIT: I have followed the advice of #LearnCocos2D and used the cleanup method, and I used it to stop a singleton object music layer to play (the MusicLayer object is initialized in AppDelegate and I meant to use it to "manage" the music across all scenes - the problem was that without stopping it in dealloc it would have kept playing the music that was loaded at init time).
-(void) loadFirstLevelData{
//HERE WILL LOAD ALL SPECIFIC ELEMENTS: ENEMIES, BONUSES etc..
//AS WELL AS THE MUSIC FOR THE LEVEL
[[MusicLayer sharedMusicLayer] _loadMusic:#"1.mp3"];
[[MusicLayer sharedMusicLayer] playBackgroundMusicFile: #"1.mp3"];
}
-(void) cleanup
{
//Should I remove all child loaded in LoadLevelData??
CCLOG(#"cleanup GameScene");
[[MusicLayer sharedMusicLayer] stopAllMusic];
//MusicLayer is not a child of GameScene but of AppDelegate - the idea is to keep loading and unloading music files - sometimes I need to keep playing the file between scenes and hence I used the singleton pattern for this as well..
[super cleanup];
}
But I still have some doubts:
Is it ok to have several loadLevelData methods in GameScene class? Each method can be 200 lines long! I tried to sublcass GameScene but is a bit messy. I explain better. I imported "GameScene.h" in the header file of the subclass and by doing so I expected that if I had ovverriden only certain methods (e.g. init) I would have been able to see the various classes imported in GameScene (e.g. InputLayerButtons). It is not the case. So I probably don't understand how imports work in Objective-C
Is it ok to remove specifc children in the cleanup method? I thought that I would remove all child that are added in the LoadLevelXXXData method to reduce the memory usage.
I have set a bounty for this question but I will probably need to test the answer and re-edit as I don't have a clear enough understanding of the subject to be super precise in the question. Hope is ok.
PS: Would be great if someone would feel like sharing a UML style diagram of a Cocos2D Shooter Game where with various levels and GameScene using singleton pattern :).
I'll focus on the questions on the bottom:
Is it ok to have several loadLevelData methods in GameScene class? Each method can be 200 lines long! I tried to sublcass GameScene but
is a bit messy. I explain better. I imported "GameScene.h" in the
header file of the subclass and by doing so I expected that if I had
ovverriden only certain methods (e.g. init) I would have been able to
see the various classes imported in GameScene (e.g.
InputLayerButtons). It is not the case. So I probably don't understand
how imports work in Objective-C
There's nothing wrong with having long methods. However I suspect your loading methods perform very similar routines, so you should check if you can generalize these into subroutines. A good indicator is if several lines of code are absolutely identical except for the parameters or variable names. The best practice is to write identical code only once, and execute it many times with varying parameters.
The #import statement is used to allow the compiler to "see" other classes. Without importing other header files, you couldn't use that class' methods and properties without the compiler complaining.
2 . Is it ok to remove specifc children in the cleanup method? I thought that I would remove all child that are added in the
LoadLevelXXXData method to reduce the memory usage.
It makes no sense to remove children during cleanup. Cocos2D removes all children during cleanup automatically.
If that does not seem to be the case for you, you have a retain cycle somewhere that prevents child nodes from deallocating.
sorry for answering this but I have been experimenting and decided to:
not use the singleton pattern for GameScene
use, insteada a singleton object to keep all shared data
My implementation draft for the GameScene (now called ShooterScene) is the following (I followed some advices in a cocos2d-iphone forum post as well as this other one ):
#import "ShooterScene.h"
#import "LevelData.h"
#import "HudLayer.h"
#interface ShooterScene (PrivateMethods)
-(void) loadGameArtFile;
#end
#implementation ShooterScene
+ (id) sceneWithId:(int)sceneId
{
CCScene *scene = [CCScene node];
ShooterScene * shooterLayer = [ShooterScene node];
[scene addChild:shooterLayer];
[shooterLayer loadGameArtFile];
LevelData * levelData = [LevelData node];
[shooterLayer addChild:levelData];
switch (sceneId) {
case 1:
[levelData loadLevelDataOne];
break;
case 2:
[levelData loadLevelDataOne];
break;
default:
break;
}
HudLayer * hud = [HudLayer node];
[hud setUpPauseMenu];
[shooterLayer addChild:hud];
return scene;
}
I am using the update method of ShooterScene to manage the all the game events (E.g. spawning, checking collisions, moving the background layer). I haven't put the full implementation here as is still work in progress, but is just to have an idea of the type of answer I am finding useful.