Forum > Beginners

Control Event Question

(1/2) > >>

JLWest:
I have 52 TImage's on a form representing a deck of cards.
Each TImage onclick event has the same code 'CardAction' coded.
During play of the game North has 13 Cards, South 13 Cards, West 13 Cards and East 13 Cards.

if it is North turn to play I would like to turn the OnClick event 'CardAction'  off for South,East and West.

After North has Played I would like to turn the OnClick event 'CardAction'  back on.

Is this possible to do in code?

Thanks.

Josh:
assuming the following
images 1-13 are north, 14-26 east, 27-39 south , 40-52 west
and you have a variable called player that holds the string north,east,south,west then a simple way would be to add the following code idea to your onclick event,


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- onclick event...var ok:boolean=false;      cardnum:integer;begin  your code to  get card number from the sender name  case cardnum of     1..13:ok:=upcase(player)='NORTH';     14..26:ok:=upcase(player)='EAST';     27..39:ok:=upcase(player)='SOUTH';         40..52:ok:=upcase(player)='WEST';  end;  if not ok then exit;  .....  your normal on cllick code;end; ;

not compileable code.. as no op code
   

JLWest:
@Josh
Thanks

Images are named I1..I52;

North,South,East and West are hands of 13 cards.
North's hand can contain 1,9,16,17,18,19,22,23,24,25,32,41 and 52
       So  1 would be I1 which is the Ace of Clubs and 9 would be I9 which is the 6 of Clubs, and so on.
 
After the distribution of the cards I always know the card number in two hands  the South hand and the Dummy hand.

I may know the cards in the other two hands if I'm replaying a saved hand from history. If this is not a replay the cards not in South's hand and the dummy hand are in the ThePool.

Every Image onclick event is coded to CardAction
CardAction decides what action need to happen to say the Ace Spades.

If it is North turn to play I don't want East,South or West cards to be active.

That's why I was wondering if there is way to turn OnClick events on and off.

 


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.CardAction(Sender: TObject);Var S: String;  Hand: HANDS;  CName: String;  aName: String;  CNo: integer=0;  aLeed: String; begin  if (sender is timage) then begin     CName := TImage(Sender).Name; {'I40'=Ace Spades}  



                       

y.ivanov:
Put the card index into the Tag property of the TImage, e.g. TImage with Name of 'I20' to have Tag := 20. Thus later you can check if the corresponding card is present into the North array. (according to https://forum.lazarus.freepascal.org/index.php/topic,59668.msg444985.html#msg444985).
Then later you can check into the 'CardAction':

   
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    type       THand = array[1..13] of Integer;      var      North, East, South, West: THand;     function IsInHand(ACard: Integer; var AHand: THand): Boolean;    var I: Integer;    begin      Result := False;      for I := Low(AHand) to High(AHand) do        if ACard = AHand[I] then          Exit(True);    end;     procedure TForm1.CardAction(Sender: TObject);    Var S: String;      Hand: HANDS;      CName: String;      aName: String;      CNo: integer=0;      aLeed: String;      Card: Integer;     begin      if (sender is timage) then begin         CName := TImage(Sender).Name; {'I40'=Ace Spades}           Card := TImage(Sender).Tag;         if It_is_North_turn and IsInHand(Card, North)  then           Play_the_Card(Card)          else           Exit;  BTW, How you handle decreasing number of cards during play? Replacing with a special card index? 0? 

As Thaddy suggested (https://forum.lazarus.freepascal.org/index.php/topic,59668.msg445159.html#msg445159) it will be a way simpler to use sets for your player hands, since the check and inclusion/exclusion of cards will be naturally handled, consider:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TCard = (1..52);  TCards = set of TCard;  THand = TCards;var    CardRecs: array[TCard] of CardRecords;    Deck: TCards;    North, East, South, West: THand;    Card: TCard;
then will be no need to use loops for checking or adding/removing:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  Include(Card, North); // add the Card to the North hand  Exclude(Card, North); // remove the Card from the North hand  if Card in North then ...; // check North player possesses the Card 
You must use a typecast when assigning from/to Image.Tag property:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Image.Tag := Integer(Card);Card := TCard(Image.Tag);

dje:
You can use 4 TPanel's (NSEW) and place 13 TImage's in each panel, you can disable OnClick's for all TImage's in a TPanel "group" by setting TPanel.Enabled = False

You can also compare the clicked TImage's Parent with each panel to identify its group.

Ideally, all the controls would be created dynamically, and destroyed during play. LCL already contains methods for BringToFront/SendToBack/Parent := .....,

So, game piece movement is mostly implemented by LCL. Yes, Tag or Name can be used for more grouping, but card placement is after all, visual.

Navigation

[0] Message Index

[#] Next page

Go to full version