order of raphael.js animations - raphael

when changing size attributes of an image along with about 100 other rect elements, the image resize happens last even though it is first element changed in my code. Image change is made first within function A, all other changes are made within a loop in function B. reference to elements stored in array 'shape'. I
function A(){
image.attr({'width':w, 'height':h});
function B();
}
function B(){
for(i=0;i>100;i+=1){
shape[i].attr({'x':x, 'y':y});
}
}

Related

updating variable between functions in c++

My main program is to generate a random number to create movement of a object in a 2 dimensional array and to keep track of it.
one of my function void current_row(int row){position = row}; keeps track of the current row of the object.
since the variable is not global. i am finding problems calling the current location and updating it to the next movement. this is how the other function may look like:
void movement (){
int row;
row = current_row();
/*
* Here is the problem i'm having. This may well be
* a third function which has the same information
* as my first function. But still how do I access
* once without modifying it and access it
* again to update it?
*/
// call another function that creates new row.
// update that info to the row
}
i am new to c++.
Use an instance variable to keep track of it. That's why instance variables exist: To hold their values between function calls.
In case it's an OOP environment (as C++ tag implies), some class should declare int row as a class member (including a getter and a setter as methods).
Another option is declaring the variable at the head of the main() part of the program and call functions with row as a function parameter.
void movement(int row)
{
}
You can consider the parameter be passed by reference if you are intending to change it, otherwise it would be better declaring it const inside the function parameter declaration. If part of the answer sounds unfamiliar to you I would suggest reading through :
What's the difference between passing by reference vs. passing by value?

Reference to non static member function must be called

I have a vector bars that contains several coloured box objects.Each box object has it's own draw and update function. Each box moves from one side of the screen to the next side. when it's outside the screen the box should be removed. I'm using iterators to move the boxes and determine when they are outside of the screen.
I'm very new to c++ and I'm having trouble getting the code to work. the function to erase an object from a vector is giving me the error Reference to non static member function must be called. I'm reading up on static and non static members but I'm still a bit lost.
here's my main header file with the relevant code
class game : public ofxiPhoneApp {
public:
void setup();
void update();
void draw();
void exit();
vector <Colorbar> bars;
bool checkBounds (Colorbar &b);
};
in my game.mm file I create the vector and iterate over it to set the properties of the coloured bar objects:
void game::setup(){
bars.assign(5, Colorbar());
for (int i = 0; i<bars.size(); i++) {
ofColor color = colors.giveColor();
bars[i].setup();
bars[i].setColor(color.r,color.g,color.b);
bars[i].setWidth(50);
bars[i].setPos(ofGetScreenHeight()-(i*50), 0);
}
}
the update function that move the bars across the screen.
void game::update(){
for(vector<Colorbar>::iterator b = bars.begin(); b != bars.end(); b++){
(*b).update();
}
//this is the part that gives the error
bars.erase((remove_if(bars.begin(), bars.end(), checkBounds),bars.end()));
}
and here's the function to check if the box is out of bounds
bool game::checkBounds (Colorbar &b){
if (b.pos.x > ofGetScreenHeight()+50) {
// do stuff with bars vector here like adding a new object
return true;
} else {
return false;
}
}
I've done some experimenting, and making the bool checkBounds (Colorbar &b);
non-static by removing it from the header file makes the code work. but the problem is that I'd also like to be able to access the bars vector in that function to add a new object when an old one is deleted. And that won't work anymore.
How can I solve this?
You need a unary functor taking a ColourBar. A member function has an implicit first parameter for this. This means it cannot be called like this:
Colorbar cb;
game::checkBounds(cb);
It needs to be bound to an instance of its class, otherwise it would not be able to access other members of that instance. So you need to bind the checkBounds member function to an instance of game. In your case, this looks like the right instance to bind:
#include <functional> // for std::bind
using std::placeholders; // for _1
...
remove_if(bars.begin(), bars.end(), std::bind(&game::checkBounds, this, _1)) ...

Accessing an array in one class from another class with C++

I'm having trouble trying to access an array used in my main class from another class. My application is an editor for making a 2d platform game - it basically allows you to place down 2D assets (segments) and build up a level.
My main class handles an array of map segment classes (each segment class in the array holds information such as position, scale and rotation of the segment on the map) and draws them to screen.
I have a separate class which is basically a panel (dragabble, and resizable like you would find in something like Photoshop) that is initialised in the main class and is used to draw a grid of available segments from a file. What I need is the ability to click on one of the segments which then adds information to the array that is referenced in the main class.
I have my main class "Map" which declares an array:
map.h (simplified)
class Map
{
public:
MapSegment* mapSeg[512];
};
I'm then trying to send a reference of that array when I create the panel to display the available segments, like so:
Panel* segmentPane = new SegmentPanel(sf::Rect<float>(200,200,250,200), mapSeg);
Segment Panel header is formed as follows:
class SegmentPanel : public Panel
{
public:
SegmentPanel(sf::Rect<float> _position, MapSegment* mapSeg[512];);
void Update();
void Draw(sf::RenderWindow & renderWindow);
void ReadSegments();
private:
std::vector<SegmentDefinition *> segDef;
MapSegment* mapSeg[512];
};
And SegmentPanel cpp:
SegmentPanel::SegmentPanel(sf::Rect<float> _position, MapSegment* mapSeg[512])
: Panel(_position)
{
panelTitle = "Segment Selection";
}
void SegmentPanel::Update()
{
// Update segments
}
void SegmentPanel::Draw(sf::RenderWindow & renderWindow)
{
// Draw default panel items
Panel::Draw(renderWindow);
// Draw segments
}
However, add elements to the array from SegmentPanel.cpp class doesn't seem to be reflected in my main Main class - it seems to create a new array in memory.
I'm still fairly new to C++ after working with C#!
First, there's no such thing as an array parameter type in C++. In your SegmentPanel constructor, the MapSegment* mapSeg[512] parameter is actually equivalent to MapSegment** mapSeg; it's just a pointer to a pointer!
Panel* segmentPane = new SegmentPanel(sf::Rect<float>(200,200,250,200), mapSeg);
Here, you attempt to pass the array mapSeg. This undergoes array-to-pointer conversion which turns it into a pointer to its first element (a MapSegment**) and then passes that pointer.
This is all fine, but you do nothing with the mapSeg argument in your constructor. If you want access to the array, you'll need to store that pointer somewhere. You can do that by changing the member of SegmentPanel called mapSeg to:
MapSegment** mapSeg;
Then change your constructor to:
SegmentPanel::SegmentPanel(sf::Rect<float> _position, MapSegment** mapSeg)
: Panel(_position), mapSeg(mapSeg)
{
panelTitle = "Segment Selection";
}
Note the initialisation of mapSeg in the member initialization list.
Another way you can do this is to take a reference to array type argument instead. Your constructor would now look like:
SegmentPanel::SegmentPanel(sf::Rect<float> _position, MapSegment* (&mapSeg)[512])
: Panel(_position), mapSeg(mapSeg)
{
panelTitle = "Segment Selection";
}
The type of the mapSeg argument is a "reference to array of 512 pointers to MapSegment". You'll then need to make the member mapSeg the same type.

std::vector::push_back fails to add data to my vector

I have these two pieces of code that are messing up without throwing any errors:
The first piece is from a custom class which I am trying to push into an array.
class idRect {
public:
sf::FloatRect rect;
int id;
idRect(int _id, sf::FloatRect _rect) : id(_id), rect(_rect) {}
};
The second piece is where the function gets called.
if((deltaX + deltaY) < 500) { //Taxi distance calculation
cout << endl << "Passed check" << endl;
gloAreas.push_back(idRect(id, entity.getGlobalBounds()));
}
gloAreas is a globally defined vector which contains idRect objects.
As said earlier I have observed from the console that "Passed check" outputs and that the size of my vector doesn't increase EDIT: globally.
Edit: The error also seems rather random and only happens for 1 in 6 instances of the objects calling the push_back functions.
I'm using SFML for the sf::FloatRect which is basically just a vector of 4 floats. getGlobalBounds() is another function from SFML that returns the bounding rectangle of a sprite in sf::FloatRect format.
Any ideas of what is going wrong?
Sincerely,
BarrensZeppelin
EDIT 2:
The error seems to have erupted due to a mix between my own incompetence and std::multiset's sorting, maybe I'll come back for that in another thread ^^ (With a sscce ofc)
Thank you guys for you time and help.
If gloAreas is defined as static, it won't be a true global. It will have global scope, but a copy of it will be created for each translation unit.
For a global, you need to declare it with extern and define it in a single implementation file.
Disclaimer: answer is just a guess, my crystal ball might be off today...
My crystal ball answer: You have redefined gloAreas in an interior scope, like this:
vector<idRect> gloAreas; // defines global
void F( vector<idRect> gloAreas ) // defines local instance
{
gloAreas.push_back(); // affects local instance
return; // destroys local instance
}
int main() {
F(gloAreas); // Copies global instance to parameter
// global remains unchanged.
}

Why is `this` equal to 0x0, causing my program to crash?

I'm designing a simple Connect 4 game. So far, I have 4 underlying classes:
Colour - responsible for representing colours (RGBA). Includes conversion operators.
Player - represents a player of the game. Each Player has a Colour and a name.
Board - represents the playing board. It contains dimensions, as well as a 2D vector of Tiles with those dimensions.
Tile - a nested class within Board. Represents one space on the board. Each Tile has a Colour and an std::unique_ptr to the owner of that tile. The owner starts as nullptr and can be changed once to a Player. The colour starts as a transparent black.
I've tested my Colour class and it appears to be working fine. My Player class is in tip-top shape as well. However, I'm having some problems with the Board/Tile classes.
My test consisted of creating two players, and a board. These executed normally. Next, I loop through the dimensions of the board, once for each tile. I then call
board.tile (j, i).claimBy (p2);
The loop goes through rows with i and columns with j, the way you'd expect to print it.
tile (j, i) retrieves the tile I'm working with. It works as expected.
Chain of Events Leading to the Crash:
claimBy (p2) sets the tile to become claimed by player 2. It is implemented as follows:
bool Board::Tile::claimBy (const Player &owner)
{
if (!_owner)
{
*_owner = owner;
_colour = owner.colour();
return true;
}
return false;
}
_owner is my std::unique_ptr<Player>. It first checks whether the owner of the tile has been set before (i.e. is not nullptr). If not, it sets the Player inside to the one passed in. It then updates the tile's colour and returns true. If the tile has been previously claimed, it returns false.
Following the debugger, the crash occurs in the line *_owner = owner;. Stepping in takes me to the line struct Player (my declaration of the Player class), which I take to be the implicit copy constructor (remember the class only has a Colour _colour and a std::string _name).
Stepping in again leads me to Colour::operator= (which makes sense for a copy constructor to call). Here's the definition:
Colour &Colour::operator= (const Colour &rhs)
{
if (*this != rhs)
{
_red = rhs.red();
_green = rhs.green();
_blue = rhs.blue();
_alpha = rhs.alpha();
}
return *this;
}
The path turns into *this != rhs. This is just a reverse call to operator==, which is:
return red() == rhs.red()
&& green() == rhs.green()
&& blue() == rhs.blue()
&& alpha() == rhs.alpha();
The first comparison here red() == rhs.red() has red() which is just return _red;. This is the point at which the program crashes. The debugger states that this (this->_red) is 0x0.
I'm clueless about why this is happening. My best guess is that I'm using the smart pointer wrongly. I've never actually used one before, but it should be pretty similar to normal pointers, and I didn't think release would accomplish anything if the pointer is nullptr.
What could be the cause of this being 0x0?
Edit:
I'm sure everything is initialized, as I do so in each constructor, in member initializers (e.g. Board::Tile::Tile() : _colour (Colours::NONE), _owner (nullptr){}), where NONE is a transparent black.
I'm also not too proficient with a debugger, as I haven't used it that much over printing debugging values.
The line
*_owner = owner;
means "make a copy of the owner object, and store it at the place that _owner points to." The problem is that _owner doesn't point to anything yet; it's still null.
If you really want to make a copy of the Player object in each tile that the player controls, you'd need to do
_owner.reset(new Player(owner));
But making copies of the Player object is a strange thing to do. Consider using shared_ptr instead — you can have both owner and _owner be shared_ptrs, and just assign one to the other in the usual way.
You start off with a default initialized std::unique_ptr<Player>. That is to say, the equivalent of a NULL pointer with some cleanup semantics. Then you try to dereference it in the statement *_owner=owner; so that you can assign to it.
Thus the statement *_owner=owner; is basically equivalent to ((Player*)NULL)->operator=(owner);, calling the implicit assignment operator. The first thing this does is then equivalent to ((Player*)NULL)->_colour=owner._colour; Finding this==NULL is not surprising here; indeed, it's expected.
The fix depends on what you actually want to happen. Should each Board::Tile be given a completely new copy of its owner? Then you want to instead say _owner.reset(new Player(owner)). Do you just want each tile to hold a reference to an already existing player? Can you guarantee that the Player object owner will outlive the Board::Tile object? Then you want a raw pointer: (in declaration of Board::Tile) Player const *_owner; (in implementation) _owner=&owner;.