Code snippets
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <vector> using namespace std; class BaseClass { public: BaseClass() {} ~BaseClass() {} virtual const string func() { return "Base class\n"; } }; class DerivedClass : public BaseClass { public: DerivedClass() {} ~DerivedClass() {} const string func() override { string s = BaseClass::func(); s += "Derived class\n"; return s; } }; int main(int argc, char** argv) { // For polymorphism to work, we need to store pointers vector<BaseClass*> objects; DerivedClass obj; objects.push_back(&obj); for (auto &o : objects) { cout << o->func().c_str(); } char in; cin >> in; return 0; } |
2D Math snippets
1 2 3 4 5 6 7 8 9 10 11 |
public function MoveTowards(angle:Number):void { nx = x + Math.cos(angle * FP.RAD) * movespeed * FP.elapsed; ny = y + Math.sin(angle * FP.RAD) * movespeed * FP.elapsed; if (!CheckCollision(nx, ny)) { x = nx; y = ny; } } |
1 2 3 4 5 6 7 |
public function GetAngleTo(x2:int, y2:int):Number { var deltaY:Number = y - y2; var deltaX:Number = x2 - x; return Math.atan2(deltaY, deltaX) * 180 / Math.PI; } |
1 2 3 4 5 6 7 8 9 10 11 |
public function update():void { if(mouseover) { image.scale += (1.2 - image.scale) / 2; } else { image.scale += (1 - image.scale) / 2; } } |
1 2 3 4 |
function updatePosition() { HXP.clampInRect(this, HXP.camera.x, HXP.camera.y, HXP.screen.width, HXP.screen.height, (width+height)); } |