c# Iterate and do operations on a generic collection concurrently
pI've created a game emulation program using c# async socks. I need to
remove/add amp; do iterations on a collection (a list that holds clients)
concurrently. I am currently using lock, however, it's a a huge
performance drop. I also do not want to use local lists/copies to keep the
list up-to-date. I've heard about ConcurrentBags, however, I am not sure
how thread safe they are for iterations (for instance if a thread removes
an element from the list while another thread is doing an iteration on
it!?)./p pWhat do you suggest? /p pstrongEdit: here is a situation/strong
this is when a packet is sent to all the users in a room/p precodelock
(parent.gameClientList) { for (int i = 0; i lt;=
parent.gameClientList.Count() - 1; i++) if
(parent.gameClientList[i].zoneId == zoneId)
parent.gameClientList[i].SendXt(packetElements); //if room matches -
SendXt sends a packet } /code/pre pWhen a new client connects /p precode
Client connectedClient = new Client(socket, this); lock (gameClientList) {
gameClientList.Add(connectedClient); } /code/pre pSame case when a client
disconnects./p pI am asking for a better alternative (performance-wise)
because the locks slow down everything./p
Thursday, 12 September 2013
menu inflating calls multiple times at fragment's onCreateOptionsMenu
menu inflating calls multiple times at fragment's onCreateOptionsMenu
I use Fragments and when I switch to nested Fragment, which implements
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) my menu
inflates quantity of times when I get to that nested Fragment. How can I
avoid this? I also implement constructor of Fragment with methods:
setRetainInstance(true);
setHasOptionsMenu(true);
When I tried to implement siple solution as:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
if(!isInflated)
{
inflater.inflate(R.menu.contacts_archive_menu, menu);
isInflated = true;
}
super.onCreateOptionsMenu(menu, inflater);
}
but my menu wasn't inflate after the screen rotation.
I use Fragments and when I switch to nested Fragment, which implements
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) my menu
inflates quantity of times when I get to that nested Fragment. How can I
avoid this? I also implement constructor of Fragment with methods:
setRetainInstance(true);
setHasOptionsMenu(true);
When I tried to implement siple solution as:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
if(!isInflated)
{
inflater.inflate(R.menu.contacts_archive_menu, menu);
isInflated = true;
}
super.onCreateOptionsMenu(menu, inflater);
}
but my menu wasn't inflate after the screen rotation.
Fill a column of a numpy array with another array
Fill a column of a numpy array with another array
I have:
x = np.zeros((96,11,11,2,10),dtype=np.float64)
y = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype=np.float64)
x[:,:,:,0,0] = y
print x[0,:,:,0,0]
i get:
[[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]]
but I want the transpose of this.
Any suggestions?
I have:
x = np.zeros((96,11,11,2,10),dtype=np.float64)
y = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype=np.float64)
x[:,:,:,0,0] = y
print x[0,:,:,0,0]
i get:
[[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.]]
but I want the transpose of this.
Any suggestions?
Wednesday, 11 September 2013
Displaying dimensions of each image as caption or label
Displaying dimensions of each image as caption or label
I want to loop through each image on a page...
For each image, get the height and width of the image, and display it as
either a caption or label or free-text, located above or below or next-to
the image. The display method (caption, label, free-text) and location
don't matter, whichever is easiest is OK.
I know how to loop through all "image" elements and call a function. I
just need to know how to get the image dimensions (is it just image.width,
image.height?), and then how to display that information on the page near
each image. I'll keep the display text simple, just "width x height",
like: 220 x 144 or 220w x 144h.
I'm not worried about making sure the images are loaded, because I'll
execute the loop after "onload", or actually, more likely, by a button
press.
I want to loop through each image on a page...
For each image, get the height and width of the image, and display it as
either a caption or label or free-text, located above or below or next-to
the image. The display method (caption, label, free-text) and location
don't matter, whichever is easiest is OK.
I know how to loop through all "image" elements and call a function. I
just need to know how to get the image dimensions (is it just image.width,
image.height?), and then how to display that information on the page near
each image. I'll keep the display text simple, just "width x height",
like: 220 x 144 or 220w x 144h.
I'm not worried about making sure the images are loaded, because I'll
execute the loop after "onload", or actually, more likely, by a button
press.
Sending TCP packets through a socket
Sending TCP packets through a socket
I'm using VpnService to capture packets and after capturing them I want to
send them to their destination. Now, the capturing aspect works. I got the
protocol, Source IP / Destination IP and the Source Port / Destination
Port from the packets.
I was thinking about creating a socket with these parameters. VpnService
has actually a method protect() which protects the socket and the traffic
will not be forwarded through VPN.
I don't have muche experience with sockets. But the other day I read a
comment here saying I only send the actual data through the socket and not
the IP or TCP header? But since TCP uses a 3-way-handshake (correct me if
i'm wrong) the first packets wouldn't have any data, just a SYN - flag.
Does that mean this method doesn't work or can i send a packet with the
header through the socket?
I'm using VpnService to capture packets and after capturing them I want to
send them to their destination. Now, the capturing aspect works. I got the
protocol, Source IP / Destination IP and the Source Port / Destination
Port from the packets.
I was thinking about creating a socket with these parameters. VpnService
has actually a method protect() which protects the socket and the traffic
will not be forwarded through VPN.
I don't have muche experience with sockets. But the other day I read a
comment here saying I only send the actual data through the socket and not
the IP or TCP header? But since TCP uses a 3-way-handshake (correct me if
i'm wrong) the first packets wouldn't have any data, just a SYN - flag.
Does that mean this method doesn't work or can i send a packet with the
header through the socket?
Looking for Client - Server code that can send string from client to server and received string as respond
Looking for Client - Server code that can send string from client to
server and received string as respond
I fount many examples, maybe someone know some good example that works (i
try to avoid editing...)
server and received string as respond
I fount many examples, maybe someone know some good example that works (i
try to avoid editing...)
MovieClip timeline animations wont play across classes - Flash Game
MovieClip timeline animations wont play across classes - Flash Game
First question. Please be gentle.
I'm making a simple (I'd hoped) keyboard controlled movieclip of a game
character. The Movieclip has five frames, each with a simple animation
inside for when the player presses the arrow keys, walking up, down,
stable etc.
I made a simple prototype in the main application class file and it works
PERFECTLY. I also traced currentFrame and totalFrames and that is correct
too. When key is pressed it plays correct frame and it says the
totalFrames are 5. Great!
However I want to make a multi level game, so have used the application
class as a level switcher and each level and object is its own class.
During this migration the MovieClip stops working. It is added to the
stage fine and can be moved with the arrow keys but the individual
animations wont play. It just flickers through each of the five keyframes.
Also when I trace it, it says currentFrame is 0 (?) and totalFrames is 1
(?)
It wont be controlled by .stop or .gotoAndPlay either.
Please help!
Here is the single class version that works fine.
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
[SWF(width="650", height="450",backgroundColor="#FFFFFF", frameRate="60")]
public class CharacterMovieclip extends MovieClip
{
//Create and initialize the vx and vy variables
public var vx:int = 0;
public var vy:int = 0;
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
public var Character:Class;
public var character:MovieClip = new Character();
public function CharacterMovieclip()
{
//Add Character
stage.addChild(character);
character.x = 200;
character.y = 100;
character.gotoAndStop(1);
//Add the event listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
character.gotoAndStop(3);
}
}
public function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT
|| event.keyCode == Keyboard.RIGHT)
{
vx = 0;
character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
{
vy = 0;
character.gotoAndStop(1);
}
}
public function enterFrameHandler(event:Event):void
{
//Move the player
character.x += vx
character.y += vy;
trace(character.currentFrame);
trace(character.totalFrames);
trace(character.x);
}
}
}
But that all goes to hell once I try it across a few classes. Here is the
character class.
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
public class Character extends MovieClip
{
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
private var CharacterImage:Class;
//Private properties
private var _characterImage:DisplayObject = new CharacterImage();
private var _character:MovieClip = new MovieClip();
//Public properties
public var vx:int = 0;
public var vy:int = 0;
public function Character()
{
_character.addChild(_characterImage);
this.addChild(_character);
}
}
}
The character object is then added to the LevelOne class
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class LevelOne extends Sprite
{
//Declare the variables to hold
//the game objects
private var _character:Character;
private var _background:Background;
//A variable to store the reference
//to the stage from the application class
private var _stage:Object;
public function LevelOne(stage:Object)
{
_stage = stage;
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(event:Event):void
{
startGame();
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function startGame():void
{
//Create Game Objects
_character = new Character();
_background = new Background();
//Add them to stage
this.addChild(_background);
_background.x = 0;
_background.y = 0;
this.addChild(_character);
_character.x = 300;
_character.y = 50;
_character.gotoAndStop(1);
//Event listeners
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
//Move the game character and check its stage boundaries
_character.x += _character.vx;
_character.y += _character.vy;
checkStageBoundaries(_character);
trace(_character.currentFrame);
trace(_character.totalFrames);
trace(_character.x);
}
private function checkStageBoundaries(gameObject:MovieClip):void
{
if (gameObject.x < 50)
{
gameObject.x = 50;
}
if (gameObject.y < 50)
{
gameObject.y = 50;
}
if (gameObject.x + gameObject.width > _stage.stageWidth - 50)
{
gameObject.x = _stage.stageWidth - gameObject.width - 50;
}
if (gameObject.y + gameObject.height > _stage.stageHeight - 50)
{
gameObject.y = _stage.stageHeight - gameObject.height - 50;
}
}
private function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
_character.vx = -5;
_character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT)
{
_character.vx = 5;
_character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP)
{
_character.vy = -5;
_character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN)
{
_character.vy = 5;
_character.gotoAndStop(3);
}
}
private function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT
|| event.keyCode == Keyboard.RIGHT)
{
_character.vx = 0;
_character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
{
_character.vy = 0;
_character.gotoAndStop(1);
}
}
}
}
And levelOne is added to the stage by the Main application class.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.MovieClip;
[SWF(width="650", height="450",
backgroundColor="#FFFFFF", frameRate="60")]
public class Main extends Sprite
{
private var _levelOne:LevelOne;
public function Main()
{
_levelOne = new LevelOne(stage);
stage.addChild(_levelOne);
}
}
}
Somewhere in all this my MovieClip stops playing the individual
animations. Its on the stage, can be moved around with arrow keys, but
wont play the keyframe animations. (And trace seems to think its playing
frame 0, and it only has 1 frame too.)
Any help would be hugely appreciated. My head hurts.
First question. Please be gentle.
I'm making a simple (I'd hoped) keyboard controlled movieclip of a game
character. The Movieclip has five frames, each with a simple animation
inside for when the player presses the arrow keys, walking up, down,
stable etc.
I made a simple prototype in the main application class file and it works
PERFECTLY. I also traced currentFrame and totalFrames and that is correct
too. When key is pressed it plays correct frame and it says the
totalFrames are 5. Great!
However I want to make a multi level game, so have used the application
class as a level switcher and each level and object is its own class.
During this migration the MovieClip stops working. It is added to the
stage fine and can be moved with the arrow keys but the individual
animations wont play. It just flickers through each of the five keyframes.
Also when I trace it, it says currentFrame is 0 (?) and totalFrames is 1
(?)
It wont be controlled by .stop or .gotoAndPlay either.
Please help!
Here is the single class version that works fine.
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
[SWF(width="650", height="450",backgroundColor="#FFFFFF", frameRate="60")]
public class CharacterMovieclip extends MovieClip
{
//Create and initialize the vx and vy variables
public var vx:int = 0;
public var vy:int = 0;
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
public var Character:Class;
public var character:MovieClip = new Character();
public function CharacterMovieclip()
{
//Add Character
stage.addChild(character);
character.x = 200;
character.y = 100;
character.gotoAndStop(1);
//Add the event listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
character.gotoAndStop(3);
}
}
public function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT
|| event.keyCode == Keyboard.RIGHT)
{
vx = 0;
character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
{
vy = 0;
character.gotoAndStop(1);
}
}
public function enterFrameHandler(event:Event):void
{
//Move the player
character.x += vx
character.y += vy;
trace(character.currentFrame);
trace(character.totalFrames);
trace(character.x);
}
}
}
But that all goes to hell once I try it across a few classes. Here is the
character class.
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
public class Character extends MovieClip
{
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
private var CharacterImage:Class;
//Private properties
private var _characterImage:DisplayObject = new CharacterImage();
private var _character:MovieClip = new MovieClip();
//Public properties
public var vx:int = 0;
public var vy:int = 0;
public function Character()
{
_character.addChild(_characterImage);
this.addChild(_character);
}
}
}
The character object is then added to the LevelOne class
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class LevelOne extends Sprite
{
//Declare the variables to hold
//the game objects
private var _character:Character;
private var _background:Background;
//A variable to store the reference
//to the stage from the application class
private var _stage:Object;
public function LevelOne(stage:Object)
{
_stage = stage;
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(event:Event):void
{
startGame();
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function startGame():void
{
//Create Game Objects
_character = new Character();
_background = new Background();
//Add them to stage
this.addChild(_background);
_background.x = 0;
_background.y = 0;
this.addChild(_character);
_character.x = 300;
_character.y = 50;
_character.gotoAndStop(1);
//Event listeners
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
//Move the game character and check its stage boundaries
_character.x += _character.vx;
_character.y += _character.vy;
checkStageBoundaries(_character);
trace(_character.currentFrame);
trace(_character.totalFrames);
trace(_character.x);
}
private function checkStageBoundaries(gameObject:MovieClip):void
{
if (gameObject.x < 50)
{
gameObject.x = 50;
}
if (gameObject.y < 50)
{
gameObject.y = 50;
}
if (gameObject.x + gameObject.width > _stage.stageWidth - 50)
{
gameObject.x = _stage.stageWidth - gameObject.width - 50;
}
if (gameObject.y + gameObject.height > _stage.stageHeight - 50)
{
gameObject.y = _stage.stageHeight - gameObject.height - 50;
}
}
private function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
_character.vx = -5;
_character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT)
{
_character.vx = 5;
_character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP)
{
_character.vy = -5;
_character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN)
{
_character.vy = 5;
_character.gotoAndStop(3);
}
}
private function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT
|| event.keyCode == Keyboard.RIGHT)
{
_character.vx = 0;
_character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
{
_character.vy = 0;
_character.gotoAndStop(1);
}
}
}
}
And levelOne is added to the stage by the Main application class.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.MovieClip;
[SWF(width="650", height="450",
backgroundColor="#FFFFFF", frameRate="60")]
public class Main extends Sprite
{
private var _levelOne:LevelOne;
public function Main()
{
_levelOne = new LevelOne(stage);
stage.addChild(_levelOne);
}
}
}
Somewhere in all this my MovieClip stops playing the individual
animations. Its on the stage, can be moved around with arrow keys, but
wont play the keyframe animations. (And trace seems to think its playing
frame 0, and it only has 1 frame too.)
Any help would be hugely appreciated. My head hurts.
Subscribe to:
Posts (Atom)